Unit 3: Programming · Conditionals

Article

Making decisions with if

5 min read

Your phone shows "Low battery" only when the battery really is low. A program that did exactly the same thing every time would be useless. Programs need to decide, and the tool for deciding is the IF statement.

An IF runs code only when a condition is true

So far, every line of your programs has run, one after another. That is called sequencing. Selection is different: the program chooses which lines run, depending on whether something is true or false.

A condition is a yes-or-no question that the computer can answer with true or false. Here is an IF statement with a condition:

AP pseudocode
temp ← 31
IF(temp > 25)
{
    DISPLAY("Hot day")
}
DISPLAY("Done")
Runs in your browser

The condition is temp > 25, which asks: "Is temp greater than 25?" The lines inside the braces run only if the answer is true. Here 31 is greater than 25, so the program shows Hot day and then Done. On the exam, the two words appear together on one line, because DISPLAY follows its value with a space. Python's print starts a new line.

Now change 31 to 20 and run the Python again. The condition is false, so the lines inside the IF are skipped. Only Done appears. The line DISPLAY("Done") is outside the braces, so it runs every time.

Python has no braces. It uses a colon after the condition and indentation (spaces at the start of the line) to show which lines are inside the IF.

Check your understanding

What is displayed when this program runs?

AP pseudocode
temp ← 30
IF(temp > 25)
{
    DISPLAY("Hot")
}
DISPLAY("Done")

What a condition looks like

A condition compares two things. For now you need three comparisons:

ConditionAsks
score > 50Is score greater than 50?
count < 3Is count less than 3?
name = "Ana"Is name equal to the text Ana?

Look at the last one. In AP pseudocode, = between two values asks whether they are equal. It does not store anything. Storing is done by the arrow ←. (In Python, a question about equality is written ==, and = stores a value. The next article covers every comparison.)

The computer checks the condition first. Only then does it decide whether to run the block:

StepWhat the computer doestemp is 31temp is 20
1Work out temp > 25truefalse
2Run the lines inside the braces?yesno
3Run DISPLAY("Done")yesyes

Check your understanding

The condition of an IF statement is false. What happens to the lines inside its braces?

Several IFs in a row

Each IF is judged by itself. Two IFs in a row can both run, or neither, or just one.

AP pseudocode
score ← 80
IF(score > 50)
{
    DISPLAY("Pass")
}
IF(score > 75)
{
    DISPLAY("Merit")
}

Both conditions are true for 80, so the program displays Pass Merit. With score ← 60 only the first is true, so it displays Pass. With score ← 40 it displays nothing.

Worked example: a discount

A shop takes 20 off any order over 100.

AP pseudocode
total ← 150
IF(total > 100)
{
    total ← total - 20
}
DISPLAY(total)
LinetotalCondition
total ← 150150
IF(total > 100)150150 > 100 is true
total ← total - 20130
DISPLAY(total)130

The program displays 130. If total started at 80, the condition would be false. The subtraction would be skipped, and the program would display 80.

Common mistakes

  • Thinking the lines in the braces always run. They run only when the condition is true.
  • Writing ← in a condition. IF(x ← 5) is not a question. To ask whether x equals 5, write IF(x = 5).
  • Treating two IFs as one choice. Each IF is checked separately, so both blocks can run.
  • Getting Python's layout wrong. Without the colon, or without the indented lines, Python gives an error or runs the wrong lines. A line that is not indented is outside the IF.
  • Skipping the check. Always work out whether the condition is true or false first, and only then decide what runs.

On the exam

You are given a program and the starting values. You work out whether each condition is true, then say what is displayed or what a variable holds. Do the condition first, every time, even when it looks obvious.

Key words

Selection: choosing which lines run, depending on a condition.

Condition: a question whose answer is true or false.

IF statement: code that runs a block of lines only when its condition is true.

Block: the lines inside the braces of an IF.

Sequencing: running lines one after another, in order.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.