vb6basicqbasicquickbasic

Rewriting Basic Code without Label


I have some code that uses label, an example of it is below:

SUB occupy (x)
occupied(x) = 0
FOR i = 1 TO 40
IF armyloc(i) = x THEN 
    occupied(x) = i
    GOTO holdup
ELSE
END IF
NEXT i
holdup:
END SUB

As you can see, it uses the label holdup to jump out of the For..Next loop it is in. I don't know a good way to change this to be better/proper code? For example, I might usually do something like this:

SUB occupy (x)
occupied(x) = 0
FOR i = 1 TO 40
IF armyloc(i) = x THEN 
    occupied(x) = i
    GOTO holdup
ELSE
       NEXT i
END IF
END SUB

But this creates a problem - the FOR..NEXT loop is now nested within an IF..THEN for half of it. Any suggestions on ways to resolve this issue?


Solution

  • As Mark Hall noted in the comments above, using Exit For works well in these situations. Remove the GOTO and replace with an Exit For statement.