Back to Challenge Set 5

Pseudocode Challenge: Conditionals

Difficulty  

divider

Objective

Read and write decisions in the notation the AP exam uses. No computer needed — this one works on paper, and doing it on paper is closer to the real thing.

This is the hardest translation in the course, and it is worth knowing that going in. The reference sheet has no else if. Every chain you write in C# has to be rebuilt as nested blocks, and every chain you read on the exam will be drawn that way.

Skills to Practice

  • Tracing an IF/ELSE and saying what it displays
  • Reading a single = as equals, not as assignment
  • Rewriting an else if chain as nested IF/ELSE blocks
  • Tracing a nested chain without getting lost in the closing braces

What You Are Allowed to Use

Everything from the exam reference sheet that we have covered so far. Nothing else appears in these problems.

Assignment
a ← expression
Display
DISPLAY(expression)
Comparison
a = b a ≠ b
a > b a < b
a ≥ b a ≤ b
Selection
IF(condition)
{
...
}
ELSE
{
...
}

Three rules that catch people:

  • Assignment is ←. A single = inside a condition means is equal to — the opposite of what it means in C#.
  • There is no ELSE IF. A chain is built by putting a whole new IF inside an ELSE block.
  • There is no string interpolation and no explicit type. A variable comes into existence the first time you assign to it.

Worked Example

C#
int age = 20;
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
Exam Notation
age ← 20
IF(age ≥ 18)
{
DISPLAY("Adult")
}
ELSE
{
DISPLAY("Minor")
}

The braces sit in the same places and ELSE is on its own line in both. Notice what disappeared: int, the semicolons, and the quotes around Console.WriteLine. Notice what changed: = became ←, and >= became ≥.


The Problems


1 — Trace

This is already in exam notation. Write down exactly what it displays.

Problem 1
temp ← 55
IF(temp > 70)
{
DISPLAY("Warm")
}
ELSE
{
DISPLAY("Cool")
}

2 — Trace

Read the condition carefully. This is the one place the two notations disagree about a single character, and reading it as C# gives the wrong answer.

Problem 2
a ← 8
b ← 8
IF(a = b)
{
DISPLAY("same")
}
ELSE
{
DISPLAY("different")
}

3 — Translate

Rewrite this C# in exam notation. Remember there is no ELSE IF — the chain has to become nested IF and ELSE blocks. Your answer will be taller than the original, and the closing braces will stack up at the bottom. That is correct, not a mistake.

Problem 3
int speed = 45;
if (speed > 70)
{
Console.WriteLine("Too fast");
}
else if (speed > 40)
{
Console.WriteLine("Fine");
}
else
{
Console.WriteLine("Too slow");
}

4 — Trace the Nest

This is what your Problem 3 answer should look like, one level deeper. Write down exactly what it displays, in order.

Problem 4
score ← 73
IF(score ≥ 90)
{
DISPLAY("A")
}
ELSE
{
IF(score ≥ 80)
{
DISPLAY("B")
}
ELSE
{
IF(score ≥ 70)
{
DISPLAY("C")
}
ELSE
{
DISPLAY("F")
}
}
}
DISPLAY("Recorded")

The trick, if you need one: find the deepest ELSE and read it as “none of the conditions above were true.” Then work out which single condition the score actually satisfies first. Two things get displayed here, and the second one is the reason the last line sits outside every brace.


Submit

  • What problems 1, 2, and 4 display
  • Your nested translation of problem 3
  • One sentence: which was harder, reading the nest or writing it, and why?

Handwritten and photographed is fine, and is the closest thing to exam conditions.

Commence Challenge