If... Then — Pseudocode

IF condition THEN ... END IF runs the lines inside it only when the condition is TRUE. A condition compares two values with >, <, >= (greater than or equal…

Here is what you will build by the end of this lesson. Mrs. Sri sends orders out with a motorbike taxi rider. Delivery is free when the order total is above $50. Your program reads the total of one order, then prints the delivery cost. For a total of 65, it prints the first line below. For a total of 50, the second:

Free delivery
Delivery 10

The program is the same, but what it prints is different. Until the previous lesson, every line of your program always ran. From now on, a program can run only some of its lines, depending on a condition.

IF ... THEN ... END IF

This program prints Free delivery only when total, the order total, is more than 50. The result is below the code:

total ← 65
IF total > 50 THEN
    PRINT "Free delivery"
END IF
PRINT "Thank you"
Free delivery
Thank you

Now change the first line to total ← 30. The condition is false, so the IF block is skipped and only Thank you is printed:

total ← 30
IF total > 50 THEN
    PRINT "Free delivery"
END IF
PRINT "Thank you"
Thank you

ELSE: when the condition is false

With ELSE, the program also has lines for when the condition is false:

total ← 30
IF total > 50 THEN
    PRINT "Free delivery"
ELSE
    PRINT "Delivery 10"
END IF
Delivery 10

The program now has two branches: the block under THEN and the block under ELSE. Exactly one branch always runs, never both. JavaScript and Python write the same two words in small letters: if and else.

Comparing two values

A condition is a comparison of two values, and its result is always TRUE or FALSE:

PRINT 65 > 50
PRINT 50 > 50
PRINT 50 >= 50
TRUE
FALSE
TRUE

These are the comparison signs you can use:

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" means > 50, so an order of exactly 50 still pays for delivery. "At least 50" means >= 50, so an order of exactly 50 already gets free delivery. One = sign changes the result for that customer, so read the rule slowly.

= compares, ← fills

Text can be compared too. In this program, code is a discount code that the customer types:

code ← "SAVE"
IF code = "SAVE" THEN
    PRINT "Discount 5"
END IF
Discount 5

code ← "SAVE" fills the variable code. code = "SAVE" inside the IF compares: is the value of code equal to SAVE? In the lesson on variables, = could replace ←. Inside a condition, = always compares.

Before you read on, guess: if the customer types save in small letters, do they still get the discount?

code ← "save"
IF code = "SAVE" THEN
    PRINT "Discount 5"
ELSE
    PRINT "Unknown code"
END IF
Unknown code

No. Two texts are equal only when every letter is the same, including capital and small letters. Later, in JavaScript and Python, you compare with == (in JavaScript usually ===), because in those two languages = always means fill.

⚠️ Why won't the IF run?

One more mistake causes no error: writing >= when you mean >. The program runs without any message, but a customer who spends exactly 50 also gets free delivery. So test your program with a number exactly at the limit.

TL;DR: IF condition THEN ... END IF runs the lines inside it only when the condition is TRUE. A condition compares two values with >, <, >= (greater than or equal to), <=, = or ≠. Add ELSE for lines that run when the condition is FALSE. Of those two groups of lines, exactly one always runs. "Above 50" means > 50, while "at least 50" means >= 50. Inside a condition, = compares, and ← (the sign that fills a variable) causes an error. Text is compared letter by letter, including capital and small letters.

⚡ Lines that only run when it's true

Write:

total ← 65
IF total > 50 THEN
    PRINT "Free delivery"
END IF
PRINT "Thank you"

Meaning: A condition is a test that is either true or false, here total > 50 (the total is greater than 50). The lines between IF ... THEN and END IF run only when the condition is true. The lines after END IF always run. The lines inside the IF are moved 4 spaces to the right so they are easy to see.

⚡ ELSE: when the condition is false

Write:

total ← 30
IF total > 50 THEN
    PRINT "Free delivery"
ELSE
    PRINT "Delivery 10"
END IF

Meaning: The lines under ELSE run when the condition is false. In this program the total is 30, so it prints Delivery 10. These two parts of the IF are called branches, and exactly one branch always runs.

⚡ Exactly at the limit

Guess first: if the first line of that program is changed to total ← 50, what does it print?

Delivery 10. 50 is not greater than 50, so 50 > 50 is FALSE and the ELSE branch runs.

Meaning: "Above 50" means > 50 (greater than). "At least 50" means >= 50 (greater than or equal to). Always test your program with a number exactly at the limit, here 50.

⚡ = compares, ← fills

Guess first: code ← "save", then IF code = "SAVE" THEN. Is the condition true?

No. Two texts are equal only when every letter is the same, including capital and small letters.

Meaning: Inside an IF condition, = compares: are the two values the same? ← fills a variable, and when you use it inside a condition, the program stops with an error.

What you will learn