Chapter 2: changing a value, typed

Chapter 2's smallest program declares a value, changes it, and changes it again from its own old value. Nothing here is structure a tool could write for you, so the IDE writes the program's shell and you type the three lines and their comments. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-mutation.

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 values.mutation

  defines program

    Mutation()
      stdout <- Stdout()

      score <- 0          //declare 'score', starting at 0
      score := 10         //change it to 10
      score := score + 5  //change it to 15

      stdout.println(`Final score is ${score}`)

//EOF

Step 1: The program's shell

Choose Code > New Construct > Program…

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

Mutation is the program's name. Type it and choose Preview.

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

The preview shows Mutation about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Mutation about to be written

The tool leaves no blank line before the program: select the Mutation() line and, above it, type the one the book opens with. Saves when typed.

The EK9 IDE once this is done: The tool leaves no blank line before the program

Step 2: The body, typed

Select the stub, from ready <- true down to its //AI line, and type the body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type the body exactly as the chapter shows it

Step 3: Run it

Choose Run > Run Program…

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

The project's only program is already chosen. Choose Run.

Final score is 15 - declared at 0, then 10, then 10 + 5. Choose Close.

The EK9 IDE just before choosing Close: Final score is 15 - declared at 0, then 10, then 10 + 5

The result

Mutation.ek9 now reads exactly as chapter 2 of Getting Started with EK9 shows it: declared with <-, changed with :=, and changed once more using its own current value.

The EK9 IDE when the lesson has finished

Where this lesson comes from