If / Else
Demonstrates conditional branching with if/else.
Example 011 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Grader has no face and no memory. Handlers: classify (line 14).
The source
011_if_else.cell
// ============================================================================
// 011 — If / Else
// ============================================================================
// Demonstrates conditional branching with if/else.
//
// Run: soma run 011_if_else.cell classify 85
// Expect:
// Score 85 is grade: B
// B
cell Grader {
// Classify a numeric score into a letter grade
on classify(score: Int) {
let grade = "F"
// if / else if / else chains
if score >= 90 {
grade = "A"
} else if score >= 80 {
grade = "B"
} else if score >= 70 {
grade = "C"
} else if score >= 60 {
grade = "D"
} else {
grade = "F"
}
print("Score {score} is grade: {grade}")
return grade
}
}What the toolchain says
$ soma check 011_if_else.cell
✓ All checks passed.$ soma run 011_if_else.cell classify 85
Score 85 is grade: B
B