Control Flow
Control-flow statements decide which code runs, how often it runs, and when the program should switch paths.
If
If <condition> Then
<statements>
[ElseIf <condition> Then
<statements> ...]
[Else
<statements>]
End If
- Then is required on If and ElseIf.
- Else must appear alone on its line.
- The terminator must be exactly End If.
An If condition must evaluate to True or False. Most conditions are built from comparisons such as score > 10 or lives = 0.
If score > highScore Then
highScore = score
End If
If health <= 0 Then
Print("Game Over")
Else
Print("Keep going")
End If
You can combine tests with And, Or, and Not.
If score >= 100 And lives > 0 Then
Print("Bonus round")
End If
If keyFound Or debugMode Then
OpenDoor()
End If
If Not finished Then
UpdateGame()
End If
If you are unsure how the condition part works, read Expressions for a beginner-friendly explanation of comparisons and operator precedence.
While
While <condition>
<statements>
Wend
- The condition is required.
- Wend must appear alone on its line.
While lives > 0 And Not finished
UpdateGame()
Wend
Do / Loop
Do
<statements>
Loop
Do While <condition>
<statements>
Loop
Do Until <condition>
<statements>
Loop
Do
<statements>
Loop While <condition>
Do
<statements>
Loop Until <condition>
- A condition may appear on Do or on Loop, but not both.
- If While or Until is present, a condition is required.
For
For <identifier> = <startExpr> To <endExpr> [Step <stepExpr>]
<statements>
Next [identifier]
- To is required.
- Step is optional.
- Next may repeat the variable name.
- If it does, the name must match the For variable.
Repeat / Until
Repeat
<statements>
Until <condition>
- Repeat must appear alone on its line.
- Until requires a condition on the same line.
Repeat
ReadInput()
Until choice <> 0
Select / Case
Select <expression>
Case <caseItem>[, <caseItem>...]
<statements>
[Case Else
<statements>]
End Select
<expression>
<startExpr> To <endExpr>
- Case Else is supported.
- Case lists may mix single values and ranges.
- The terminator must be exactly End Select.