If... Then — Pseudocode

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…

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

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?

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.

What you will learn