Control Flow

Control flow statements allow you to control the execution path of your program based on conditions and loops.



If / ElseIf / Else

Execute code based on conditions:

Basic If:

If score > 100 Then
    Print "High score!"
EndIf

If-Else:

If lives > 0 Then
    Print "Still alive"
Else
    Print "Game Over"
EndIf

If-ElseIf-Else:

If score >= 90 Then
    Print "Grade: A"
ElseIf score >= 80 Then
    Print "Grade: B"
ElseIf score >= 70 Then
    Print "Grade: C"
Else
    Print "Grade: F"
EndIf

Single-line If:

If x > 10 Then Print "Greater than 10"


Select / Case

Choose between multiple options based on a value:

Select day
    Case 1
        Print "Monday"
        Exit
    Case 2
        Print "Tuesday"
        Exit
    Case 3
        Print "Wednesday"
        Exit
    Case 4
        Print "Thursday"
        Exit
    Case 5
        Print "Friday"
        Exit
    Case 6, 7
        Print "Weekend!"
        Exit
    Default
        Print "Invalid day"
        Exit
EndSelect

Important: Each Case block needs Exit to prevent fall-through (like C++ switch statements).

Multiple values per case:

Select grade$
    Case "A", "B"
        Print "Excellent!"
        Exit
    Case "C"
        Print "Good"
        Exit
    Case "D", "F"
        Print "Needs improvement"
        Exit
EndSelect


While Loop

Repeat code while a condition is true (checks condition before each iteration):

Local count:Int = 0
While count < 10
    Print "Count: " & ToString(count)
    count = count + 1
Wend

Exit loop early:

While True
    Local input$ = Input("Enter command (quit to exit): ")
    If input = "quit" Then
        Exit  ; Exit the loop
    EndIf
    Print "You entered: " & input
Wend


Until Loop

Repeat code until a condition becomes true (opposite of While):

Local count:Int = 0
Repeat
    Print "Count: " & ToString(count)
    count = count + 1
Until count >= 10

The loop body always executes at least once, since the condition is checked at the end.



For / Next Loop

Iterate a specific number of times with a counter:

Basic For loop:

For i:Int = 1 To 10
    Print "Iteration: " & ToString(i)
Next

Step value:

; Count by 2s
For i:Int = 0 To 20 Step 2
    Print i
Next

; Count backwards
For i:Int = 10 To 1 Step -1
    Print i
Next

Nested loops:

For y:Int = 0 To 9
    For x:Int = 0 To 9
        Print x & "," & y
    Next
Next


Forever Loop

Create an infinite loop that runs until explicitly broken:

Forever
    ; Game main loop
    UpdateGame()
    DrawGame()

    If KeyHit(KEY_ESCAPE) Then
        Exit  ; Exit the loop
    EndIf
Forever

Forever loops are useful for game loops and event loops that run continuously.



Exit Statement

Use Exit to break out of any loop early:

For i:Int = 1 To 100
    If i = 50 Then
        Exit  ; Stop at 50
    EndIf
    Print i
Next


Continue Statement

Use Continue to skip to the next iteration:

For i:Int = 1 To 10
    If i Mod 2 = 0 Then
        Continue  ; Skip even numbers
    EndIf
    Print i  ; Only prints odd numbers
Next


Examples

BambooBasic © 2026 Michael Denathorn