Back

Pseudocode Challenge: Combining Conditions

divider

Objective

Four problems on Boolean expressions. AND, OR and NOT are one of the few places the exam and Lua agree almost word for word — almost, and problem 3 is the exception.

Do this on paper. There is nothing to run and nothing to type. On exam day you get this notation on a printed sheet and no computer, so practicing it any other way practices the wrong thing.


Reference Card

Everything you need for this page. This is the same notation the College Board uses on the exam.

Exam Notation — what you have so far
a ← expression assign a copy of the result to a
DISPLAY(expression) show the value, FOLLOWED BY A SPACE
INPUT() take a value from the user
+ - * / 17 / 5 is 3.4 (no truncation)
a MOD b remainder. 17 MOD 5 is 2
RANDOM(a, b) whole number from a to b, BOTH INCLUDED
= ≠ > < ≥ ≤ comparison. = means EQUALS
NOT / AND / OR boolean operators
IF(condition) condition is TRUE or FALSE. Nothing else.
{ ... } no ELSE IF exists - nest an IF inside an ELSE
ELSE
{ ... }

Read NOT last and read it out loud. NOT (x < y) is not "x is greater than y" — it is "it is not the case that x is less than y", and the two are only the same answer when the values are different.


The Problems


Problem 1 — trace

What does this display?

Problem 1
age ← 16
hasPass ← false
IF(age ≥ 18 OR hasPass)
{
DISPLAY("Enter")
}
ELSE
{
DISPLAY("Denied")
}

Then answer this: hasPass holds a value, and it is not being compared to anything. Say what kind of value it is and why OR accepts it on its own.


Problem 2 — trace

Three separate IF statements, one after another. What does this display?

Problem 2
x ← 12
y ← 5
IF(NOT (x < y))
{
DISPLAY("first")
}
IF(x > 10 AND y > 10)
{
DISPLAY("second")
}
IF(x > 10 OR y > 10)
{
DISPLAY("third")
}

Not all three run. Not none of them run either.


Problem 3 — Lua, and the one place this breaks

This is Lua, not exam notation. Write down exactly what it prints, then answer the question underneath.

Problem 3
local count = 0
if count then
print("A")
end
if count == 0 then
print("B")
end

The question: the first if is handed the number 0, not a comparison. Say what Lua does with it, and then say why there is no way to write that first if in exam notation at all.

Look at the reference card again. IF takes a condition, and a condition on the exam is true or false. The sheet never says what IF(0) does because on the exam that cannot happen — and a habit you build in Lua will quietly follow you into May if nobody names it.

Problem 4 — trace

RANDOM does not give one answer, so write down every value this could display.

Problem 4
n ← RANDOM(1, 4)
DISPLAY(n * 2)

The endpoints are the whole question. Check the reference card before you decide how many answers there are.


Submit

Your four answers, plus the two short explanations from problems 1 and 3. For the traces, write the output exactly as it appears, including spacing.

Worth 3 points, graded on completion. Show your working for the traces — a wrong answer with visible reasoning is worth more to both of us than a right one with none.

Commence Challenge