Skip to content

Example: classify a range

A complete Prismio program using a for range, modulo, conditions, and mutable accumulation.

Last verified

The range 1..10 visits 1 through 10, both ends included. This program sums the even values.

prismio
import std.io

fn main() -> Int {
    let mut total = 0
    for value in 1..10 {
        if (value % 2 == 0) {
            total += value
        }
    }
    println(total)
    return 0
}

Expected output: 30.

Why it works

total is declared mut because compound assignment changes the binding. 1..10 includes its end; 1..<11 would visit the same values, stopping before 11. % 2 computes the remainder and == 0 produces the Boolean required by if.

The compiler infers each ordinary integer as Int, keeping %, ==, and += on compatible exact types. Mixing U8 or I64 into the expression would require explicit casts.

Variation: count matches

Replace total += value with total += 1 to count even values rather than sum them. The expected result becomes 5. This variation preserves the same control-flow structure while changing only the accumulator invariant.

Boundaries demonstrated

  • for value in 1..10 is an integer range, not collection iteration.
  • if is a statement and does not return the selected value.
  • The condition must be Bool; if (value % 2) is invalid.
  • Loop and branch bodies establish nested lexical scopes.
  • return 0 reports success separately from printed program output.