Chapter 3: a switch of comparisons, folded
Chapter 3's Grade turns a score into a word with switch: below the pass mark is Fail, below the merit mark Pass, below the distinction mark Merit, and the rest Distinction. This lesson writes the Fail print and the three marks first, plain, then lets Surround With ▸ switch fold it into a switch. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-grade.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module decisions defines program Grade() -> score as Integer stdout <- Stdout() passMark <- 40 meritMark <- 60 distinctionMark <- 75 switch score case < passMark stdout.println("Fail") case < meritMark stdout.println("Pass") case < distinctionMark stdout.println("Merit") default stdout.println("Distinction") //EOF
Step 1: A program with a parameter
Choose Code > New Construct > Program…
Grade is the program's name. Add one parameter, score of type Integer. Choose Preview.
The preview shows Grade with its parameter. Choose Apply.
Select the Grade() line and, above it, type the blank line the book opens its program with. Saves when typed.
Step 2: A plain start
Select the stub, from require score? down to its //AI line, and type the plain start: a Stdout, the three marks, and the one Fail print. Saves when typed.
Step 3: Fold it under a switch
Put the caret on the Fail print, right-click - or choose Refactor > Refactor This… - and choose Surround With ▸ switch.
Select the stub, from switchSubject <- Integer(0) down to its //AI line, and type switch score - what to switch on. Saves when typed.
Select the case line and type its test: case < passMark - a comparison, not a listed value. Saves when typed.
Step 4: The other cases
Select the Fail print and below it type the two further cases, Pass below the merit mark and Merit below the distinction mark. Saves when typed.
Select the default's stub, from defaultComplete <- Boolean(true) down to its //AI line, and type what default prints - everyone at or above the distinction mark. Saves when typed.
Step 5: Run it
Choose Run > Run Program…
Type 70 into Arguments (space-separated), then choose Run.
Merit - 70 is not below the pass mark of 40 or the merit mark of 60, but is below the distinction mark of 75. Choose Close.
The result
Grade.ek9 now reads exactly as chapter 3 of Getting Started with EK9 shows it: a switch whose cases are comparisons, tried in order - the first one that holds wins - with a default for the rest. Surround With ▸ switch folded the plain Fail print under the switch head; you typed the subject, the comparisons and the other branches. AI assistants use ek9_surround_with with surroundType switch.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_add_program,ek9_surround_with.
In the EK9 reference: Flow Control in EK9














