Control Flow

Control-flow statements decide which code runs, how often it runs, and when the program should switch paths.

Back to Language Reference


If

If <condition> Then <statements> [ElseIf <condition> Then <statements> ...] [Else <statements>] 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
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>

For

For <identifier> = <startExpr> To <endExpr> [Step <stepExpr>] <statements> Next [identifier]

Repeat / Until

Repeat <statements> Until <condition>
Repeat
    ReadInput()
Until choice <> 0

Select / Case

Select <expression> Case <caseItem>[, <caseItem>...] <statements> [Case Else <statements>] End Select <expression> <startExpr> To <endExpr>