🌱 Beginner's Guide lesson 4 of 12

Making Decisions

Up to now, every program ran every line, top to bottom, no matter what. Real programs make choices: if the player has no lives left, show "Game over"; otherwise, keep playing. In this lesson your programs learn to decide.

Asking questions

A decision starts with a question whose answer is yes or no: a Boolean, ok (true) or ort (false). These comparison operators ask questions about two values:

operator question
a == b is a equal to b?
a != b is a not equal to b?
a < b is a less than b?
a <= b is a less than or equal to b?
a > b is a greater than b?
a >= b is a greater than or equal to b?
[ try-it.sts ]
bongSlanhOun "questions";
akthe age = 11;
jongyeytha age == 11;
jongyeytha age != 11;
jongyeytha age >= 13;
jongyeytha "mango" == "mango";
[ what it prints ]
ok
ort
ort
ok

Remember: one = puts a value in a box, two == asks whether two things are equal.

ber: if

ber means if. It runs a block of code only when a question comes out ok:

[ try-it.sts ]
bongSlanhOun "if";
akthe temperature = 36;
ber (temperature > 30) {
    jongyeytha "It's hot! Drink some water.";
}
jongyeytha "Have a nice day.";
[ what it prints ]
It's hot! Drink some water.
Have a nice day.

The question goes inside round brackets ( ), and the code to run goes inside curly braces { }. Everything between a { and its matching } is called a block. We indent (push to the right with spaces) the lines inside a block. steav2 doesn't care about the spaces, but they make it much easier for humans to see what's inside what.

Change 36 to 20 and run it again: the first message disappears, but "Have a nice day." is still printed, because it's outside the block.

minjengte: else

minjengte means else (or "otherwise"). It gives ber a second block, for when the answer is ort:

[ try-it.sts ]
bongSlanhOun "else";
akthe lives = 0;
ber (lives > 0) {
    jongyeytha "Keep playing!";
} minjengte {
    jongyeytha "Game over :(";
}
[ what it prints ]
Game over :(

Exactly one of the two blocks runs, never both, never neither.

More than two choices

To choose between several options, put another ber straight after minjengte. steav2 checks each question in order and runs the first block whose answer is ok:

[ try-it.sts ]
bongSlanhOun "grades";
akthe score = 72;
ber (score >= 90) {
    jongyeytha "A: amazing!";
} minjengte ber (score >= 70) {
    jongyeytha "B: great job!";
} minjengte ber (score >= 50) {
    jongyeytha "C: you passed!";
} minjengte {
    jongyeytha "Let's practise some more.";
}
[ what it prints ]
B: great job!

72 is also bigger than 50, but only the first matching block runs.

Combining questions: ng, reu and !

Sometimes one question isn't enough. You can combine them:

  • ng means and: a ng b is ok only if both are ok.
  • reu means or: a reu b is ok if at least one is ok.
  • ! means not: it flips ok into ort and back.
[ try-it.sts ]
bongSlanhOun "combine";
akthe age = 15;
akthe hasTicket = ok;
ber (age >= 13 ng hasTicket) {
    jongyeytha "Enjoy the movie!";
}
akthe day = "Saturday";
ber (day == "Saturday" reu day == "Sunday") {
    jongyeytha "It's the weekend!";
}
ber (!hasTicket) {
    jongyeytha "Please buy a ticket first.";
}
[ what it prints ]
Enjoy the movie!
It's the weekend!

The last message isn't printed: hasTicket is ok, so !hasTicket is ort.

Questions must really be questions

Some languages let you write ber (5) and guess what you meant. steav2 doesn't guess: the thing inside ber ( ) must be a Boolean.

[ x_x broken on purpose ]
bongSlanhOun "not a question";
akthe coins = 3;
ber (coins) {
    jongyeytha "I have coins";
}
[ what it prints ]
[line 3] Error at 'coins': expected Boolean but got LekThom

What you probably meant was ber (coins > 0), which is a real yes-or-no question.

Your turn

  1. Make a variable number and print positive, negative or zero depending on its value. Hint: ber, minjengte ber, minjengte.
  2. A theme park ride needs you to be at least 120 cm tall and at least 8 years old. Write a program with height and age variables that prints whether you can ride. Hint: ng.
  3. Print Nice! if a variable color is "pink" or "purple", and Hmm. otherwise.

Solution for exercise 1:

[ try-it.sts ]
bongSlanhOun "sign";
akthe number = -4;
ber (number > 0) {
    jongyeytha "positive";
} minjengte ber (number < 0) {
    jongyeytha "negative";
} minjengte {
    jongyeytha "zero";
}
[ what it prints ]
negative
  • steav2♡ help
  • Y2K✓ ready
  • wasminside
  • .stsvalid

best viewed at 1024x768 ☆ made with ♡ ☆ the reference is steav2's own docs