Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Flow of Control

A program runs its statements from top to bottom until something changes that order. tiko has two families of statement that do: ones that choose which block runs (if, select case) and ones that repeat a block (for, do ... loop, for each). This program uses one of each:

'' Choosing and repeating: the statements most programs are made of.

'' for counts; if chooses; continue for skips to the next pass.
dim as int32 total = 0
for n as int32 = 1 to 10
    if n mod 2 = 0 then continue for     '' leave out the even numbers
    total += n
next
print "sum of the odd numbers 1 to 10:"; total

'' do loop repeats while a condition holds.
dim as int32 side = 1
do while side * side < 50
    side += 1
loop
print "first square over 50:"; side * side

'' select case compares one value against several cases.
select case total
case is < 10
    print "the sum is small"
case 10 to 30
    print "the sum is medium"
case else
    print "the sum is large"
end select

Output

sum of the odd numbers 1 to 10: 25
first square over 50: 64
the sum is medium

Every block statement ends with a matching keyword: end if, end select, next for a for, loop for a do. Conditions need no parentheses, and the body of a block is not limited to one statement.

StatementUse it toPage
if / elseif / elsechoose between a few conditionsif/else
select casecompare one value against many casesselect case
for ... nextcount through a rangefor
do ... looprepeat while (or until) a condition holdsdo loop
exit / continueleave a loop, or skip to its next passexit and continue
for eachvisit every element of an arrayfor each
deferrun a statement when a block is leftdefer

Activities

  1. Change the first loop so that it adds up the even numbers instead.
  2. Make the do loop find the first cube over 500.