AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/3/3-6)
Unit 3 · Topic 3.6
3.6 Conditionals
Selection lets a program make decisions: run some code only when a condition is true, or choose between two paths. This topic covers the exam's IF and IF-ELSE statements and how they change the normal top-to-bottom flow of a program.
Key terms
- selection
- conditional statement
- condition
- IF
- ELSE
Selection
Selection means choosing which parts of an algorithm run based on whether a condition is true or false. In everyday terms: "If it's raining, take an umbrella; otherwise, wear sunglasses."
A conditional statement, or if-statement, is how code does selection. It changes the sequential flow of control: instead of every line running in order, some lines are skipped depending on a Boolean expression.
IF
The block inside an IF runs only if the condition is true. If it's false, the block is skipped and nothing happens; the program moves on.
IF (condition)
{
<block of statements>
}
In the block-based version on the exam, the IF block wraps around the statements it controls.
IF-ELSE
IF-ELSE always runs exactly one of its two blocks: the first if the condition is true, the second if it's false.
IF (age < 12 OR age ≥ 65)
{
price ← 8
}
ELSE
{
price ← 14
}
DISPLAY(price)
With age 30, the condition is false OR false, which is false, so price is 14. With age 70, it's true, so price is 8. The boundaries matter: age 12 pays 14 because 12 < 12 is false, and age 65 pays 8 because 65 ≥ 65 is true.
Two IFs are not the same as IF-ELSE
Two separate IF statements are checked one after the other, and both can run. The second one sees any changes the first one made. An IF-ELSE runs exactly one block. This difference is a favorite exam trap, shown in the second worked example.
You can also describe selection without code. "If the cart total is over $35, shipping is free; otherwise it costs $6" is an algorithm with selection, written in plain English. In a flowchart, a decision is usually drawn as a diamond with a "true" arrow and a "false" arrow leaving it.
Turning a rule into code
To write a conditional from a description, find three things: the condition, what happens when it's true, and what (if anything) happens when it's false.
"A phone shows a warning when its charge is below 20 percent" has a condition (charge < 20), an action when true (show the warning) and nothing when false, so a plain IF fits:
IF (charge < 20)
{
DISPLAY("Low battery")
}
If the rule had said "otherwise, show the charge," you'd add an ELSE block. If no ELSE is needed, don't add an empty one.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Tracing IF-ELSE at the boundaries
Using the ticket-price code above, what is displayed for ages 11, 12, 64 and 65?
Show the solutionHide the solution
- Step 1: Age 11: 11 < 12 is true, so the condition is true. Price 8.
- Step 2: Age 12: 12 < 12 is false and 12 ≥ 65 is false. Condition false, so price 14.
- Step 3: Age 64: both parts false. Price 14.
- Step 4: Age 65: 65 ≥ 65 is true. Price 8.
Answer: 8, 14, 14 and 8
- Example 2
Two IFs versus IF-ELSE
What does each segment display? Segment 1:
points ← 50 IF (points > 40) { points ← points - 20 } IF (points > 20) { points ← points - 20 } DISPLAY(points)Segment 2:points ← 50 IF (points > 40) { points ← points - 20 } ELSE { points ← points - 20 } DISPLAY(points)Show the solutionHide the solution
- Step 1: Segment 1: points starts at 50. 50 > 40 is true, so points becomes 30.
- Step 2: The second IF is a separate statement, checked next with the new value: 30 > 20 is true, so points becomes 10. It displays 10.
- Step 3: Segment 2: 50 > 40 is true, so the first block runs and points becomes 30. The ELSE block is skipped because exactly one block runs. It displays 30.
Answer: Segment 1 displays 10; segment 2 displays 30.
Common mistakes
- Assuming only one of two separate IF statements can run. Each IF is checked on its own, using the current values.
- Forgetting that a false IF (with no ELSE) does nothing at all.
- Getting the boundary wrong. Test the exact cutoff value: is it included or not?
On the exam
- Expect to trace conditionals for given inputs and to pick the code that matches a rule described in words. Try a value right at each boundary.
- Your Create task procedure must include selection. On the written response, be ready to explain what your IF checks and what happens in each case.
Connected topics
Videos
Check yourself
4 questions on 3.6 Conditionals. Pick an answer to see if you got it, and why.
An online store uses the following code segment to work out a shipping cost. What is displayed when total is 50 and express is true?
cost ← 5
IF (total > 50)
{
cost ← 0
}
IF (express)
{
cost ← cost + 10
}
DISPLAY(cost)
The variable num holds a positive integer. Which code segment displays "even" if num is even and displays "odd" otherwise?
Which statement assigns the same value to hot as the following code segment, for every number temp?
IF (temp > 30)
{
hot ← true
}
ELSE
{
hot ← false
}
In the exam's pseudocode, what happens when the condition in an IF (condition) statement with no ELSE evaluates to false?
0 of 4 answered