Boolean Expressions, Conditionals, and Iteration
Your school's roller coaster trip has a sign: "You must be at least 48 inches tall." A worker checks each rider: yes or no. Then the next rider, and the next, until the line is empty. That is a computer program in real life. A question with a yes/no answer, a choice based on it, and a check that repeats. In this page you will learn all three, and at the end you will return to the roller coaster.
Boolean expressions
A Boolean expression is an expression whose value is either true or false. Nothing else, no "maybe."
You build them with relational operators, which compare two values:
| Operator | Meaning | Example (x ← 7) |
Value |
|---|---|---|---|
= |
equal to | x = 7 |
true |
≠ |
not equal to | x ≠ 7 |
false |
> |
greater than | x > 5 |
true |
< |
less than | x < 5 |
false |
≥ |
greater than or equal to | x ≥ 7 |
true |
≤ |
less than or equal to | x ≤ 6 |
false |
Notice that in AP pseudocode = means "is equal to?" (a question). Assignment, which stores a value, uses the arrow ←.
You can combine Boolean values with logical operators:
-
NOT aflips the value: true becomes false, false becomes true. -
a AND bis true only if both are true. -
a OR bis true if at least one is true.
Some expressions are logically equivalent, meaning they always have the same value. Two useful ones: NOT (a AND b) is the same as (NOT a) OR (NOT b), and NOT (a OR b) is the same as (NOT a) AND (NOT b). Notice that negating flips each part and also swaps AND with OR.
Let A be true and B be false. Evaluate each expression: a) A AND B b) A OR B c) NOT B d) NOT (A AND B)
Let A be true and B be false. Evaluate each expression:
- a)
A AND B
- b)
A OR B
- c)
NOT B
- d)
NOT (A AND B)
Let x ← 7. Evaluate each as true or false: a) (x > 5) AND (x < 10) b) (x < 3) OR (x MOD 2 = 1) c) NOT (x = 7)
Let x ← 7. Evaluate each as true or false:
- a)
(x > 5) AND (x < 10)
- b)
(x < 3) OR (x MOD 2 = 1)
- c)
NOT (x = 7)
A roller coaster rule says: a rider may board if height ≥ 48 AND (age ≥ 12 OR withAdult), where height is in inches and withAdult is true or false. Evaluate the rule for…
A roller coaster rule says: a rider may board if height ≥ 48 AND (age ≥ 12 OR withAdult), where height is in inches and withAdult is true or false.
Evaluate the rule for each rider:
| Rider | Height (in.) | Age | With adult |
|---|---|---|---|
| Chloe | 50 | 10 | false |
| Malik | 52 | 13 | false |
| Camila | 45 | 15 | true |
Give true or false for each, and then write a Boolean expression that says when a rider is not allowed to board.
Selection with IF and ELSE
Selection lets a program choose which code runs, depending on a Boolean expression.
IF (condition) {
block A
}
ELSE {
block B
}
If the condition is true, block A runs. Otherwise, block B runs. The ELSE part is optional. Without it, nothing happens when the condition is false.
You can put an IF inside another one. This is a nested conditional. Nesting is how you choose among more than two outcomes.
a) What is displayed for score equal to 95, 85, 80, and 79? b) If the first condition were changed to score > 90, which score value would give a different result? What…
IF (score ≥ 90)
{
DISPLAY("A")
}
ELSE
{
IF (score ≥ 80)
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}
- a) What is displayed for
scoreequal to 95, 85, 80, and 79?
- b) If the first condition were changed to
score > 90, which score value would give a different result? What would be displayed?
Iteration with REPEAT
Iteration means repeating a block of code. AP pseudocode gives you two main loops:
-
REPEAT n TIMES { … }runs the body exactlyntimes. -
REPEAT UNTIL (condition) { … }checks the condition before each pass. If it is false, the body runs again. When it becomes true, the loop stops. If the condition is true from the start, the body never runs.
FOR EACH item IN aList { … } runs the body once for every element of a list, with item holding the current element. For example, if aList is [4, 7, 9], then FOR EACH item IN aList { DISPLAY(item * 2) } displays 8, 14, and 18. You will use it a lot with lists.
The best way to understand a loop is to hand trace it: make a table and update each variable step by step.
A loop that never stops is an infinite loop. It happens when the condition can never become true. For example:
n ← 1
REPEAT UNTIL (n < 0) {
n ← n * 2
}
Here n only grows, so n < 0 is never true. Two common causes of infinite loops: forgetting to update the variable that the condition checks, and using an update that "jumps over" the stopping value.
a) How many times is "Go" displayed by REPEAT 4 TIMES { DISPLAY("Go") }? b) What is the final value of n?
- a) How many times is
"Go"displayed byREPEAT 4 TIMES { DISPLAY("Go") }?
- b) What is the final value of
n?
n ← 0
REPEAT 5 TIMES
{
n ← n + 2
}
a) What is displayed? b) How many times does the loop body run? c) What would happen if the condition were n < 0 instead?
n ← 1
REPEAT UNTIL (n > 50)
{
n ← n * 2
}
DISPLAY(n)
- a) What is displayed?
- b) How many times does the loop body run?
- c) What would happen if the condition were
n < 0instead?
Three programmers wrote loops that are supposed to display 1, 2, 3, 4, 5. Version A: Version B: Version C: a) For each version, give the output or say that it never…
Three programmers wrote loops that are supposed to display 1, 2, 3, 4, 5.
Version A:
i ← 1
REPEAT UNTIL (i = 5)
{
DISPLAY(i)
i ← i + 1
}
Version B:
i ← 1
REPEAT UNTIL (i = 6)
{
DISPLAY(i)
i ← i + 1
}
Version C:
i ← 1
REPEAT UNTIL (i = 4)
{
DISPLAY(i)
i ← i + 2
}
- a) For each version, give the output or say that it never stops.
- b) Explain what is wrong with Version A and how to fix it.
- c) Explain why Version C is an infinite loop, and describe a condition that would make it stop safely.
Putting it together
Real programs combine all three ideas: a loop repeats, and inside it a conditional decides what to do with each item. This is the pattern behind counting, adding up only some values, and searching.
a) What is displayed, and how many times does the loop body run? b) What would happen if the line i ← i + 1 were removed? Explain. c) Describe the change needed so that…
i ← 1
total ← 0
REPEAT UNTIL (i > 10)
{
IF (i MOD 3 = 0)
{
total ← total + i
}
i ← i + 1
}
DISPLAY(total)
- a) What is displayed, and how many times does the loop body run?
- b) What would happen if the line
i ← i + 1were removed? Explain.
- c) Describe the change needed so that the program adds up the even numbers from 1 to 10 instead.