Decisions with if / else
Making programs branch: if/else in AHA! Lang is an expression that produces a value.
Decisions with if / else
An interesting program must be able to make decisions: "if it rains, bring an umbrella". In AHA! Lang, decisions are written with if and else.
Basic Form
let nilai = 80;
if nilai >= 70 {
print_str("Lulus\n");
} else {
print_str("Tidak lulus\n");
}Structure:
if <condition> {
...code if the condition is true...
} else {
...code if the condition is false...
}- The condition is an expression that produces
1(true) or0(false) — usually a comparison. - The
elseblock can be omitted if it's not needed.
if / else Is an Expression
Here's AHA! Lang's quirk: if/else produces a value. That value is the result of the last expression in the branch that runs.
let x = 10;
let y = 20;
let terbesar = if x > y { x } else { y };
print(terbesar); // 20No variable is filled inside a branch — the branch produces its value directly. This makes the program shorter and clearer.
Compare it to the usual imperative style (which also works):
let terbesar = 0;
if x > y {
terbesar = x;
} else {
terbesar = y;
}Both ways are valid, but the expression version avoids the initial terbesar value that could be misunderstood.
Nested Branches
An else branch can contain another if — for tiered decisions:
let nilai = 85;
// grade: A if >= 85, B if >= 70, otherwise C
let grade = if nilai >= 85 {
"A"
} else {
if nilai >= 70 {
"B"
} else {
"C"
}
};
print_str(grade + "\n"); // ANote: each branch returns a string, so the variable grade has type String.
Combining Conditions
Combine multiple conditions with && and ||:
let umur = 20;
let punya_ktp = 1;
if umur >= 17 && punya_ktp == 1 {
print_str("Boleh membuat KTP\n");
}AHA!'s Compositional Value System
Because if is an expression and logic results are Int, everything can be chained freely:
let x = 42;
// "Fizz" inspiration from FizzBuzz: multiples of 3
let label = if x % 3 == 0 { "Fizz" } else { "Bukan Fizz" };
print_str(label + "\n");This is one of the things that makes AHA! Lang feel like a "composable alphabet".
Exercises
- Write a program that prints "Genap" or "Ganjil" based on a number variable (hint:
angka % 2). - Create a function
tanda(n)that returns"Positif"ifn > 0,"Negatif"ifn < 0, and"Nol"ifn == 0. (Hint: nested branches.) - Use
ifas an expression: store in a variablediskon—10iftotal > 100000, otherwise0— then printtotal - diskon.
On to Looping with while