Program Flow
A program runs from the top down. These statements change that: they choose between pieces of code, or repeat them.
If
If runs code only when a condition is true. On one line, everything after Then belongs to it, and an Else can follow on the same line. Over several lines, the block ends with EndIf (or End If), and ElseIf checks further conditions in turn:
If lives = 0 Then GameOver()If score > best Then best = score Print "New best!"ElseIf score = best Then Print "Equal best"Else Print "Try again"EndIf
Select
Select compares one value with several Cases and runs the first one that matches, or the Default if none does. A Case can list several values, and strings work as well as numbers:
Select key$ Case "w", "W" y = y - 1 Case "s", "S" y = y + 1 Default Print "Use W and S"End Select
For ... Next
For counts a variable from one value to another, running the lines down to Next each time. Step changes how much it counts by, and can be negative to count down, or a fraction when the variable is a float. When the loop finishes, the variable is one step past the end.
For i = 1 To 10 Print iNextFor t# = 1.0 To 0.0 Step -0.25 Print tNext
For ... Each goes through every object of one of your own types; see Custom Types.
While and Repeat
While ... Wend repeats for as long as a condition is true, checking before each time round, so it may not run at all. Repeat ... Until checks after each time round, so it always runs at least once. Repeat ... Forever never stops by itself: it is the usual shape of a game's main loop.
While Not KeyHit(KEY_ESCAPE) Cls DrawGame() FlipWendRepeat n = Rand(1, 6)Until n = 6
Exit
Exit leaves the loop it is in straight away, carrying on after the end of the loop. In loops inside loops, it leaves only the innermost one.
Goto and Gosub
Goto jumps to a label. Gosub jumps to one too, but remembers where it came from, and Return goes back there. They are kept for older programs: functions do the same jobs more clearly.
End
End stops the program, wherever it is.
