Control flow

Every block can be written with a closing keyword or with braces. The examples show both.

If

OpenConsole()
For n = 1 To 3
  If n = 1
    PrintN("one")
  ElseIf n = 2
    PrintN("two")
  Else
    PrintN("many")
  EndIf
Next

Output:

one
two
many

C style, with optional brackets around the condition:

if (n == 1) {
  PrintN("one")
} else if n == 2 {
  PrintN("two")
} else {
  PrintN("many")
}

For

For counts from a start value to an end value, inclusive. Step sets the increment, which must be a constant and may be negative.

OpenConsole()
line$ = ""
For i = 10 To 0 Step -5
  line$ + Str(i) + " "
Next
PrintN(line$)

Output:

10 5 0 

The end value is checked before each pass, so a loop whose start is already past the end runs zero times.

C style: for (init; condition; step) { … }. Each part is a statement, so declarations and i++ work:

OpenConsole()
sum = 0
for (int i = 1; i <= 100; i++) {
  sum += i
}
PrintN(Str(sum))

Output:

5050

While

Checks the condition first, so the body may run zero times.

OpenConsole()
n = 1
While n < 100
  n * 2
Wend
PrintN(Str(n))

Output:

128

Repeat / do

Runs the body first, then checks.

Form Repeats
Repeat … Until cond until cond is true
Repeat … ForEver forever. Leave with Break
do { … } while cond while cond is true. while must be on the same line as }
OpenConsole()
n = 0
Repeat
  n + 3
Until n > 10
PrintN(Str(n))

Output:

12

Break and Continue

Break leaves the innermost loop. Break 2 leaves two nested loops. Continue jumps to the next pass of the innermost loop.

OpenConsole()
found$ = ""
For y = 1 To 9
  For x = 1 To 9
    If x * y = 42
      found$ = Str(x) + "x" + Str(y)
      Break 2
    EndIf
  Next
Next
PrintN(found$)

Output:

7x6

Select (no fall-through)

Runs the first matching Case only. A Case can list several values and ranges.

OpenConsole()
For code = 1 To 12 Step 4
  Select code
    Case 1
      PrintN("one")
    Case 2, 3, 4
      PrintN("small")
    Case 5 To 8
      PrintN("medium")
    Default
      PrintN("large")
  EndSelect
Next

Output:

one
medium
large

Inside a Select, Break leaves the enclosing loop, as in PureBasic.

Select also works on strings (compared exactly, case included) and on floating-point values, ranges included:

OpenConsole()
For i = 1 To 4
  Select StringField("apple,kiwi,plum,fig", i, ",")
    Case "apple", "plum"
      PrintN("tree fruit")
    Case "a" To "j"
      PrintN("early in the alphabet")
    Default
      PrintN("something else")
  EndSelect
Next

t.d = 21.5
Select t
  Case 0.0 To 15.9
    PrintN("cold")
  Case 16.0 To 24.9
    PrintN("mild")
  Default
    PrintN("hot")
EndSelect

Output:

tree fruit
something else
tree fruit
early in the alphabet
mild

switch (C fall-through)

switch is for C code. Cases fall through into the next one unless you Break, and Break leaves the switch, not the loop.

OpenConsole()
level = 2
out$ = ""
switch level {
  case 3: out$ + "c"
  case 2: out$ + "b"
  case 1: out$ + "a"
          Break
  default: out$ + "?"
}
PrintN(out$)

Output:

ba

For new code, prefer Select.

Goto and Gosub

Goto label jumps to label:. Gosub label calls a piece of main code that ends with Return.

OpenConsole()
n = 0
Gosub Bump
Gosub Bump
PrintN(Str(n))
End

Bump:
  n + 1
  Return

Output:

2

End

End stops the program. End code sets the exit code.