Skip to content
FutureBlitz

    Install

    The tour, 3 of 11

    Control flow

    If, For, While, Repeat, Loop, Select, and Defer, each block with its End.

    Every block ends with End and its kind: End If, End For, End While. There’s no Next, Wend, or EndIf, and no Goto.

    If

    Import std.core
    
    Local hp = 15
    
    If hp <= 0
        Print("dead")
    ElseIf hp < 20
        Print("hurt")
    Else
        Print("fine")
    End If
    
    If hp < 20 Then Print("find a medkit")     // one line takes Then, and no End If
    Output
    hurt
    find a medkit

    &&, ||, and ! are logical and stop as soon as they know the answer; the bitwise operators are &, |, ^, and ~.

    For

    To counts up to and including its end, and Until stops before it. For ... In goes over an array, a range, a Map, or anything with an iterator, and can take the index too.

    Import std.core
    
    For i = 1 To 3
        Write("{i} ")
    End For
    Print()
    
    For i = 0 Until 10 Step 3
        Write("{i} ")
    End For
    Print()
    
    Local names = ["Ada", "Grace", "Alan"]
    For i, name In names
        Print("{i}: {name}")
    End For
    
    Print((1 To 10).Sum())
    Output
    1 2 3 
    0 3 6 9 
    0: Ada
    1: Grace
    2: Alan
    55

    While, Repeat, and Loop

    Import std.core
    
    Local n = 27
    Local steps = 0
    While n != 1
        If n % 2 == 0 Then n = n / 2 Else n = 3 * n + 1
        steps += 1
    End While
    Print("27 takes {steps} steps")
    
    Local tries = 0
    Repeat                      // the body runs at least once
        tries += 1
    Until tries >= 3
    Print("tried {tries} times")
    
    Local frame = 0
    Loop                        // until a Break
        frame += 1
        If frame == 5 Then Break
    End Loop
    Print("stopped at frame {frame}")
    Output
    27 takes 111 steps
    tried 3 times
    stopped at frame 5

    Break and Continue take a loop’s label to leave or go on with an outer one: Outer: For y = ... then Break Outer.

    Select

    Import std.core
    
    For roll In [1, 4, 6, 9]
        Select roll
            Case 1
                Print("{roll}: a miss")
            Case 2 To 5
                Print("{roll}: a hit")
            Case 6, 7, 8
                Print("{roll}: a critical")
            Default
                Print("{roll}: off the table")
        End Select
    End For
    Output
    1: a miss
    4: a hit
    6: a critical
    9: off the table

    Defer

    Defer runs a statement when its block ends, however it ends, for what has to be closed or put back.

    Import std.core
    
    Function Load()
        Print("open the level")
        Defer Print("close the level")
        Print("read the level")
    End Function
    
    Load()
    Output
    open the level
    read the level
    close the level

    Next: Functions.

    Next: Functions

    Report an issue with this page or with FutureBlitz on GitHub.