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? |
bongSlanhOun "questions";
akthe age = 11;
jongyeytha age == 11;
jongyeytha age != 11;
jongyeytha age >= 13;
jongyeytha "mango" == "mango"; 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:
bongSlanhOun "if";
akthe temperature = 36;
ber (temperature > 30) {
jongyeytha "It's hot! Drink some water.";
}
jongyeytha "Have a nice day."; 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:
bongSlanhOun "else";
akthe lives = 0;
ber (lives > 0) {
jongyeytha "Keep playing!";
} minjengte {
jongyeytha "Game over :(";
} 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:
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.";
} 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:
ngmeans and:a ng bisokonly if both areok.reumeans or:a reu bisokif at least one isok.!means not: it flipsokintoortand back.
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.";
} 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.
bongSlanhOun "not a question";
akthe coins = 3;
ber (coins) {
jongyeytha "I have coins";
} [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
- Make a variable
numberand printpositive,negativeorzerodepending on its value. Hint:ber,minjengte ber,minjengte. - A theme park ride needs you to be at least 120 cm tall and at least 8 years old. Write a program with
heightandagevariables that prints whether you can ride. Hint:ng. - Print
Nice!if a variablecoloris"pink"or"purple", andHmm.otherwise.
Solution for exercise 1:
bongSlanhOun "sign";
akthe number = -4;
ber (number > 0) {
jongyeytha "positive";
} minjengte ber (number < 0) {
jongyeytha "negative";
} minjengte {
jongyeytha "zero";
} negative