BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.6
3.6 Conditionals
Selection means the program chooses which code to run based on a condition. IF and IF/ELSE are how it does that.
What you need to know
- Selection is one of the three building blocks of algorithms (with sequencing and iteration). It lets a program take different paths.
IF (condition) { block }runs the block only when the condition is true; otherwise it skips it and continues.IF (condition) { block1 } ELSE { block2 }runs exactly one of the two blocks — block1 if true, block2 if false. Never both, never neither.- After the conditional finishes, execution continues with the next statement in sequence regardless of which branch ran.
- Two separate
IFstatements are independent — both conditions are checked and both blocks can run. AnIF/ELSEis mutually exclusive. This difference is a frequent exam trap. - The condition must be a Boolean expression. Everything from 3.5 applies.
Worked example
temp ← 72
IF (temp > 80)
{
DISPLAY("hot")
}
IF (temp > 60)
{
DISPLAY("warm")
}
ELSE
{
DISPLAY("cold")
}
First IF: 72 > 80 is false → nothing. Second IF/ELSE: 72 > 60 is true → warm, and the ELSE is skipped. Output: warm. Had the first IF been true, both "hot" and "warm" would display, because they're independent statements.
Exam tip: Count the IFs. Independent IFs can each fire; an IF/ELSE chain fires exactly one branch. When a question asks "how many lines are displayed," this distinction is usually the whole question.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- An
IFwith noELSEmeans "maybe do this." AnIF/ELSEmeans "do exactly one of these two." The difference is whether there's a case where nothing in the structure runs. - The condition is evaluated once, when the IF is reached. If the block changes variables that were in the condition, that doesn't re-trigger anything — the decision was already made.
- Two
IFstatements in sequence are independent. Both conditions are checked; zero, one, or both blocks may run. This is the most common structure the exam uses to test whether you understand independence vs. mutual exclusion. IF/ELSEcan be rewritten as twoIFstatements with opposite conditions —IF (x > 5)andIF (x ≤ 5)— and the behavior is identical as long as the first block doesn't change x. If it does, the second IF might see a different x. Equivalence questions exploit this.- The exam's block-based pseudocode draws IF/ELSE as connected shapes. Read it the same way: one condition, two branches, exactly one runs.
- Selection combined with iteration — an IF inside a loop — is how counting, filtering, and finding patterns work. The IF picks which iterations "count."
Mistakes that cost points
- Reading two IFs as an IF/ELSE. If both conditions can be true for the same input, both blocks run. Count outputs accordingly.
- Reading an IF/ELSE as two IFs. Exactly one branch runs. If a question asks "which values cause both DISPLAY statements to run" and they're in an IF/ELSE, the answer is "none."
- Forgetting execution continues after the conditional. Whatever's after the closing brace runs regardless of which branch was taken.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 What is displayed by the following code?
score ← 85
IF (score ≥ 90)
{
DISPLAY("A")
}
ELSE
{
IF (score ≥ 80)
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}Show answer
Answer: B. 85 ≥ 90 is false, so the ELSE runs. Inside, 85 ≥ 80 is true, so "B" is displayed and the inner ELSE is skipped.
Q2 Consider the following code, where
n is a positive integer.
IF (n MOD 2 = 0)
{
DISPLAY("even")
}
IF (n > 10)
{
DISPLAY("big")
}For which value of n are two words displayed?Show answer
Answer: D. Both independent IFs must be true: even AND greater than 10. Only 12 qualifies.
Key vocabulary
- Selection
- choosing which code to execute based on a Boolean condition
- Conditional statement
- an IF or IF/ELSE statement