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.

The EK9 IDE as the lesson opens

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…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

Grade is the program's name. Add one parameter, score of type Integer. Choose Preview.

The EK9 IDE just before choosing Preview: Grade is the program's name

The preview shows Grade with its parameter. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Grade with its parameter

Select the Grade() line and, above it, type the blank line the book opens its program with. Saves when typed.

The EK9 IDE once this is done: Select the Grade() line and, above it, type the blank line the book opens its program with

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.

The EK9 IDE once this is done: Select the stub, from require score? down to its //AI line, and type the plain start

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.

The EK9 IDE once this is done: 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.

The EK9 IDE once this is done: Select the stub, from switchSubject <- Integer(0) down to its //AI line, and type switch score - what to switch on

Select the case line and type its test: case < passMark - a comparison, not a listed value. Saves when typed.

The EK9 IDE once this is done: Select the case line and type its test

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.

The EK9 IDE once this is done: Select the Fail print and below it type the two further cases, Pass below the merit mark and Merit below the distinction mark

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.

The EK9 IDE once this is done: 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

Step 5: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

Type 70 into Arguments (space-separated), then choose Run.

The EK9 IDE just before choosing Run: 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 EK9 IDE just before choosing Close: Merit - 70 is not below the pass mark of 40 or the merit mark of 60, but is below the distinction mark of 75

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.

The EK9 IDE when the lesson has finished

Where this lesson comes from