Choices: if, else and switch
You will learn
The forms of if the compiler accepts, the if expression, and switch with else.
if (a > b) { ... } else { ... } runs one of two blocks. The parentheses are optional: if a > b { ... } also compiles. if can be an expression too: return if (a > b) a else b; gives the larger one. switch (m) { 0 => 10, else => 20 } picks a value by matching m, and else covers every other case.
Try it
Add a case 1 => 15 to the switch in the player and check the verdict.

Every form of if and switch the t27 compiler accepts, each checked with the real compiler. Lesson 14 of the t27 basics course.
specs/basics/14_if_and_else.t27
// SPDX-License-Identifier: Apache-2.0
; t27 basics, lesson 14: Choices: if, else and switch.
; if picks one of two paths; switch picks one of many by value.
module basics_14_if_and_else;
pub fn larger(a: u8, b: u8) -> u8 {
if (a > b) {
return a;
} else {
return b;
}
}
pub fn larger_expr(a: u8, b: u8) -> u8 {
return if (a > b) a else b;
}
pub fn price(m: u8) -> u8 {
return switch (m) {
0 => 10,
else => 20,
};
}
pub const LIMIT : u8 = 10;
test the_limit_is_ten {
assert LIMIT == 10;
}
All lessons
Module 1 · A spec file
What a .t27 file holds: a module line, prose lines and comments.
Module 2 · Values and types
Constants, the width of a whole number, and true, false and text.
Module 3 · Arrays, trits and expressions
Lists of values, the three values of a trit, and what operators compute.
Module 4 · Tests and functions
A test block, several small tests in one spec, and a function with typed inputs.
Module 5 · Inside a function
Names with let and var, choices with if and switch, and loops.
Module 6 · Visibility and your own types
What pub shares with other modules, and the structs and enums you declare.
Module 7 · Modules, rules and gen-ts
Reading another module, stating a rule that always holds, and a spec turned into TypeScript.
Module 8 · The compiler and tri
The backends t27c writes, the stages it reads a spec in, and the tri command.
Module 9 · Errors, a program, what next
Reading a compiler error, one small complete program, and the course to take next.