Exercises: Boolean Expressions, Conditionals, and Iteration
Practice★☆☆
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)
Practice★☆☆
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)
Practice★☆☆
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
}
Practice★★☆
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?
Practice★★☆
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?
Practice★★☆
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.
Practice★★★
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.
Practice★★★
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.