Assign different labels based on a score using if/else.
← Control Flow · Ref: Q1153
Declare variable, assign in each branch:
label <- "Unknown" if score > excellentThreshold label: "Excellent" else if score > goodThreshold label: "Good" else label: "Needs work"
EK9 has no return. Assign in each branch using the : operator. See Q1146 for if-guard.
Example
defines module qa.controlflow.ifelsevalue defines program IfElseValueDemo() stdout <- Stdout() excellentThreshold <- 90 goodThreshold <- 70 scores <- [95, 85, 72, 60] for score in scores label <- "Unknown" if score > excellentThreshold label: "Excellent" else if score > goodThreshold label: "Good" else label: "Needs work" stdout.println(`Score ${score}: ${label}`)
Common mistakes
E01072 — EK9 has no return statement. Declare a variable before the if/else and assign in each branch.
Incorrect:
if score > 90 return "Excellent"
Correct:
label <- "Unknown" if score > excellentThreshold label: "Excellent"
Other ways to ask this
- Write code to categorize a numeric score into grade labels
- I have a score and need to assign Excellent, Good, or Needs Work based on thresholds
- Given a score from 0-100, produce a text label based on ranges
- In Java I'd use if/else if/else to assign a grade. Write the EK9 equivalent
Coming from another language?
Java: if/else if/else with return. Python: if/elif/else. Rust: let label = if { } else { }. EK9: declare variable, then assign in each branch.
Keywords: grade, label, branch, score, assign, if, threshold, else