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.
| Statement | Use it to | Page |
|---|---|---|
if / elseif / else | choose between a few conditions | if/else |
select case | compare one value against many cases | select case |
for ... next | count through a range | for |
do ... loop | repeat while (or until) a condition holds | do loop |
exit / continue | leave a loop, or skip to its next pass | exit and continue |
for each | visit every element of an array | for each |
defer | run a statement when a block is left | defer |
Activities
- Change the first loop so that it adds up the even numbers instead.
- Make the
doloop find the first cube over 500.