This tutorial requires the development conditional branch. It builds on variables and the boolean rules. The published v0.0.1 interpreter does not support this syntax.
bind score = 73;
var grade = "pending";
if score >= 90 {
grade = "A";
} elif score >= 60 {
grade = "B";
} else {
grade = "C";
}
print(grade);
Output:
B
Conditions are explicit booleans. score >= 90 compares integers and produces
a boolean. An integer such as if score {} is a type error. Use readable and,
or, and not to combine decisions. No parentheses are required around a
condition, but braces are required around every body.
Scarp checks conditions in order and runs the first matching body. Later
conditions and bodies are skipped. Use elif for another condition. else is
optional and runs only when no earlier condition matched. An if without else
simply continues after all false conditions. There is no semicolon after }.
bind label = "outer";
if true {
bind label = "selected";
print(label);
}
print(label);
Output:
selected
outer
Each body introduces a lexical scope. Its local declarations do not escape and
cannot be read in a sibling body or a later elif condition. Assigning to an
outer mutable variable, as with grade, persists after the body ends. Declaring
a local with the same name, as with label, creates a distinct binding.
if false {
print(1 / 0);
} else {
print("safe");
}
Output:
safe
The unselected arithmetic is not evaluated. Names and types are checked in every
body, however: replacing 1 / 0 with an unknown variable still fails compilation.
This catches misspelled variables even in code paths not selected by this run.
scarp check examples/decisions.srp validates the same selected path silently.
If selected arithmetic fails, earlier print statements produce no output. The
language continues to prepare the complete selected result before writing.
See the conditional design for
precise branch ordering, limits, and Rust API changes.