Conditions and loops

View source

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.

actus
verb main() {
    erg speed = 85;
    erg raining = true;
    if speed > 100 {
        println(string: "too fast");
    } else if speed > 60 && raining {
        println(string: "slow down in the rain");
    } else if speed > 60 {
        println(string: "fast");
    } else {
        println(string: "fine");
    }
    if raining || speed == 0 {
        println(string: "wet or stopped");
    }
}
bash
actus run
text
slow down in the rain
wet or stopped
process exited with status 0

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:

actus
verb main() {
    erg n = 1;
    if n {
        println(string: "yes");
    }
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1063] at 3:8: `if` condition must be `Bool`, found `Int`

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:

actus
verb main() {
    erg temperature = 18;
    erg label = if temperature > 25 {
        "warm"
    } else if temperature > 10 {
        "mild"
    } else {
        "cold"
    };
    erg level: Int = if temperature > 25 {
        3
    } else {
        1
    };
    println(string: label);
    println(int: level);
}
bash
actus run
text
mild
1
process exited with status 0

An if used as a value needs an else, and every branch must have the same type. If they differ, the compiler tells you:

actus
verb main() {
    erg n = 1;
    erg x = if n == 1 {
        5
    } else {
        "no"
    };
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1025] at 3:13: type mismatch for `branches` in `if`: expected `Int`, found `String`

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.

actus
verb main() {
    for erg i in 0..4 {
        println(int: i);
    }
    erg total = 0;
    for erg n in 1..6 {
        total += n;
    }
    println(int: total);
}
bash
actus run
text
0
1
2
3
15
process exited with status 0

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:

actus
verb main() {
    erg total: u32 = 0u32;
    for erg i: u32 in 1..5 {
        total += i;
    }
    println(int: total as Int);
}
bash
actus run
text
10
process exited with status 0

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:

actus
verb main() {
    for erg i in 5..5 {
        println(string: "never printed");
    }
    for erg i in 4..2 {
        println(string: "never printed");
    }
    println(string: "end");
}
bash
actus run
text
end
process exited with status 0

Skip and stop

continue jumps to the next round of the nearest loop. break leaves it.

actus
verb main() {
    for erg i in 0..6 {
        if i == 1 {
            continue;
        }
        if i == 4 {
            break;
        }
        println(int: i);
    }
}
bash
actus run
text
0
2
3
process exited with status 0

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:

actus
verb main() {
    erg tries = 0;
    loop {
        tries += 1;
        println(int: tries);
        if tries == 3 {
            break;
        }
    }
    println(string: "stopped");
}
bash
actus run
text
1
2
3
stopped
process exited with status 0

There is no while

Some languages have a while loop. Actus 0.3.0 does not. The word is not valid in a program:

actus
verb main() {
    erg n = 0;
    while n < 3 {
        n += 1;
    }
}
bash
actus check
text
/home/you/hello/src/main.act: error[E0009] at 3:5: `while` is not an Actus keyword or loop form; write `loop { ... }` and leave it with `break`, or use `for erg index in start..end { ... }` or `repeat erg index in start .. end { ... }`

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:

actus
verb main() {
    erg n = 0;
    loop {
        if n >= 3 {
            break;
        }
        println(int: n);
        n += 1;
    }
}
bash
actus run
text
0
1
2
process exited with status 0

What you learned

  • if, else if and else choose between blocks. The condition must be a Bool.
  • &&, || and ! combine conditions.
  • if can give a value when it is part of a line like erg x = if ...;. It then needs an else, and the branches must have the same type.
  • for erg i in a..b counts from a up to, but not including, b.
  • loop repeats until a break. continue starts the next round.
  • There is no while in 0.3.0.

Next: Verbs.