Question Do While Loop not exiting

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Perhaps I'm just tired but...

Have this very simple loop

VB.NET:
Expand Collapse Copy
Dim vFormatText As String = "~"
            Dim ReturnString As String = ""
            Dim vLoop As Integer = 1
            Do While vFormatText <> "/" Or vFormatText <> "" Or vFormatText <> "}" Or vStartIndex >= TextString.Length
                vFormatText = TextString.Substring(vEndIndex + vLoop, 1)
                ReturnString += vFormatText
                vStartIndex = vEndIndex + vLoop
                vLoop += 1
            Loop
            vString.Append(Converter(ReturnString))
            vStartIndex = vEndIndex + vLoop
            vEndIndex = TextString.IndexOf("/", vStartIndex)

And although I can clearly see in debug that vFormatString is passing string.empty ("") the code still keeps looping!

Any ideas?
 
When either of those expressions are True the loop continues, so it will never end.
VB.NET:
Expand Collapse Copy
[U]vFormatText <> "/"[/U] Or [U]vFormatText <> ""[/U]
when vFormatText is empty string these two expressions evaluate like this, with the combined result equals True. So you have Do While True always.
VB.NET:
Expand Collapse Copy
[U]True[/U] Or [U]False[/U], [B]which equates to True[/B]
Read about it in help: Or Operator (Visual Basic)
resolution:
VB.NET:
Expand Collapse Copy
Do Until vFormatText = "/" OrElse vFormatText = ""...
 
Back
Top