Here's what you'll build by the end of this lesson. Mrs. Sri sends orders out with a motorbike taxi rider, and delivery is free when the order is above Rp 50,000. Your program reads one order and answers. For an order of 65000 it writes the first line below, for an order of 50000 the second:
Free delivery
Delivery 10000
Same program, two different answers. Up to the last lesson, every line you wrote always ran. From now on your program can choose which lines run.
IF ... THEN ... END IF
total ← 65000
IF total > 50000 THEN
PRINT "Free delivery"
END IF
PRINT "Thank you"
Free delivery
Thank you
IFis followed by a condition, thenTHEN. The lines below it, down toEND IF, run only when the condition is true.END IFcloses the block. The lines after it run as usual, whatever the condition.- The lines inside the block are moved in by 4 spaces. The computer doesn't require it, but it makes it easy for you to see what belongs to the
IF.
Change the first line to total ← 30000 (the order total), and only Thank you is printed:
total ← 30000
IF total > 50000 THEN
PRINT "Free delivery"
END IF
PRINT "Thank you"
Thank you
ELSE: when the condition is false
total ← 30000
IF total > 50000 THEN
PRINT "Free delivery"
ELSE
PRINT "Delivery 10000"
END IF
Delivery 10000
The lines under ELSE run when the condition is false. Of the two branches, exactly one always runs: never both, never neither. JavaScript and Python write the same two words in small letters, if and else.
Comparing two values
A condition is a comparison, and its result is TRUE or FALSE:
PRINT 65000 > 50000
PRINT 50000 > 50000
PRINT 50000 >= 50000
TRUE
FALSE
TRUE
| Sign | Meaning |
|---|---|
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
= |
equal to |
≠ |
not equal to (!= or <> work too) |
"Above 50,000" means > 50000, so an order of exactly 50000 doesn't get free delivery yet. "At least 50,000" means >= 50000. One = sign can move a customer from paying for delivery to free, so read the rule slowly.
= compares, ← fills
Text can be compared too:
code ← "SAVE"
IF code = "SAVE" THEN
PRINT "Discount 5000"
END IF
Discount 5000
code ← "SAVE" fills code (a voucher code). code = "SAVE" inside the IF asks: does it hold SAVE or not? Before reading on, guess: if the customer types save in lower case, do they get the discount?
code ← "save"
IF code = "SAVE" THEN
PRINT "Discount 5000"
ELSE
PRINT "Unknown code"
END IF
Unknown code
They don't. Two texts only count as equal when every letter matches, capitals included. In JavaScript and Python later, comparing uses == (JavaScript prefers ===), because = there always fills.
⚠️ Why won't the IF run?
THENis missing.IF total > 50000stops with "Line 2: the IF condition must be followed by THEN."END IFis missing. The computer stops at theIFline: "Line 2: this IF is never closed." The next line of the message even says where: "Add END IF after line 3."←inside the condition.IF code ← "SAVE" THENstops with "Line 2: in a condition, compare with=, not←."- The body on the same line.
IF total > 50000 THEN PRINT "Free delivery"stops with "Line 2: start a new line after THEN." The body of a block always goes on its own lines. - The sign the wrong way round.
total => 50000stops with "Line 2: pseudocode uses>=, not=>."
One more that causes no error: >= when you meant >. The program runs fine, only a customer who spends exactly 50000 gets free delivery too. Try your program with a number right on the limit.
TL;DR: IF condition THEN ... END IF runs its body only when the condition is TRUE. Add ELSE for a body that runs when the condition is FALSE, and of the two branches exactly one always runs. Conditions compare with >, <, >=, <=, = or ≠. "Above 50,000" is > 50000, "at least 50,000" is >= 50000. Inside a condition, = compares, and ← there is an error. Text is compared letter by letter, capitals included.