Re: Example using a boolean variable with a loop

by Michael Calkins (Login MCalkins)
Moderator

 

Aye, that would work, but as I mentioned in the other post, something like this:


FOR x = 1 TO 50
            IF UCASE$(states$(x)) = UCASE$(state$) THEN
                            ok = 1
                            found = x
                            EXIT FOR
            END IF
NEXT x
IF ok = 1 THEN
            PRINT state$; " was found at index "; x
ELSE
            PRINT state$; " was not found"
END IF


could be also be written as something like this:


FOR x = 1 TO 50
            IF UCASE$(states$(x)) = UCASE$(state$) THEN
                            PRINT state$; " was found at index "; x
                            EXIT FOR
            END IF
NEXT x
IF x = 51 THEN
            PRINT state$; " was not found"
END IF


because x will be 51 if and only if EXIT FOR was not executed. The NEXT x increments x, then checks to see if x <= 50. When x is 50, NEXT x turns it to 51, then exits the loop. However, if a match had been found, EXIT FOR would exit the loop before x becomes 51.

Regards,
Michael

Posted on May 12, 2012, 7:55 PM

Respond to this message   

Return to Index

Response TitleAuthor and Date
Good shortcut. Another example using duplicate data:Solitaire on May 13
 going a bit off topic...Michael Calkins on May 13
  Yes, in VB.NET:Solitaire on May 13

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement