Conditions and loops
Choose with if, else if and else, use if as a value, and repeat work with for and loop.
A program often has to choose, or to do something many times. This page shows
how to do both: if for choices, and for and loop for repetition.
Choose with if
An if runs its block when the condition is true. else if and else add
more choices. The condition goes straight after if, with no parentheses.
The blocks always use braces.
Only the first block whose condition is true runs. Here speed > 100 is
false, and speed > 60 && raining is true, so the second message is printed
and the rest of the chain is skipped.
The comparison operators are ==, !=, <, <=, > and >=. To combine
conditions, use && for and, || for or and ! for not. && and ||
stop early: when the left side already decides the answer, the right side is
not evaluated.
The condition must be a Bool
A condition has to be a Bool, which is true or false. A number is not
accepted as a condition. Write the comparison you mean:
The message names the problem: the condition n is an Int and should be a
Bool. The fix is if n == 1 {.
if as a value
An if can also produce a value. Put it on the right side of =. Each
branch ends with an expression and no semicolon, and that expression becomes
the value:
An if used as a value needs an else, and every branch must have the same
type. If they differ, the compiler tells you:
Compare the two uses. The if in the first example does something and has
no value. The if in the second example is part of a line that creates a
variable, so it has to give a value.
Repeat with for
A for loop counts through a range. 0..4 means 0, 1, 2 and 3. The start is
included and the end is not.
for erg i in 0..4 creates a variable i for the loop. Its type comes from
the start of the range. A plain 0 makes it an Int. The loop variable
exists only inside the loop, so two loops in a row can both use i.
The range does not have to start at 0. The second loop adds 1 to 5 and prints the total, 15.
To choose another type for the counter, write it after the name. Plain numbers in the range then take that type:
The values of a range must fit the counter's type. A range that is empty or runs backwards does not run the body at all:
Skip and stop
continue jumps to the next round of the nearest loop. break leaves it.
The loop skipped 1 with continue and stopped at 4 with break.
Repeat until you say stop
When you do not know in advance how many rounds you need, use loop. It
repeats forever, so the body must contain a break:
There is no while
Some languages have a while loop. Actus 0.3.0 does not. The word is not
valid in a program:
The error names the loop forms that Actus has instead. It also lists repeat,
a counting loop that this guide does not cover.
Use loop with an if and break, or a for loop when you know the range.
This program does what while n < 3 would do:
What you learned
if,else ifandelsechoose between blocks. The condition must be aBool.&&,||and!combine conditions.ifcan give a value when it is part of a line likeerg x = if ...;. It then needs anelse, and the branches must have the same type.for erg i in a..bcounts fromaup to, but not including,b.looprepeats until abreak.continuestarts the next round.- There is no
whilein 0.3.0.
Next: Verbs.