AP CSP Topic 3.9 Guided Notes - Developing Algorithms
Big Idea 3: Algorithms and Programming · Topic 3.9 · Guided Notes (Student)
Developing Algorithms — Guided Notes
Fill these in during class or catch up here if you were absent. Print this page or work on paper — then check yourself with the CFUs on the Topic 3.9 page.
Print these notesAll CSP topics
Day 1: Same Answer, Different Path
Today’s objectives
- Compare two algorithms to decide whether they yield the same result, the same side effect, both, or neither — and explain why similar-looking algorithms can still differ (LO AAP-2.L)
- Rewrite a conditional statement as an equivalent Boolean expression, and a Boolean expression as an equivalent conditional statement (LO AAP-2.L)
Bell ringer
Two teammates each wrote a snippet to award a 'Perfect Run' badge. Both start with finished ← true and noDamage ← true. Snippet A: IF(finished) { IF(noDamage) { badge ← true } }. Snippet B: IF(finished) { badge ← true } IF(noDamage) { badge ← true }. Trace both with those values, then trace both again with finished ← true, noDamage ← false.
Write 2–4 sentences: For which inputs do A and B agree, and for which do they DISAGREE? The two snippets look almost the same — what one structural difference makes them behave differently?
01. Same Result, Same Side Effect?
Key Vocabulary (LO AAP-2.L)
| Term | Definition (write it) |
|---|---|
| Algorithm | |
| Result | |
| Side effect | |
| Equivalent algorithms | |
| Boolean expression |
One Task, Many Algorithms — Result vs Side Effect
- To compare two algorithms you must check two things: whether they compute the same .
- The CED states that different algorithms can be developed or used to .
- Two algorithms can agree on the result yet still differ because .
Result Is the Value; Side Effect Is What Happens
Result
Side effect
- The RESULT of an algorithm is .
- A SIDE EFFECT is .
AP TIP: 'Same result' means same final value. 'Same side effect' means same observable behavior (usually what is displayed). A question can turn on either one.
Find the Higher Score
One concrete algorithm: compare two scores and display the larger. Its RESULT is 95; its SIDE EFFECT is printing 95.
AP Pseudocode (what the exam tests)
scoreA ← 88
scoreB ← 95
IF(scoreA > scoreB) { higher ← scoreA }
ELSE { higher ← scoreB }
DISPLAY(higher)
Python (the runnable version)
scoreA = 88
scoreB = 95
if scoreA > scoreB:
higher = scoreA
else:
higher = scoreB
print(higher)
Output
95
Run and edit this yourself in the coding exercises for this topic.
Similar-Looking, Different Behavior
Two snippets each try to report the higher score. Algo A: IF(a > b){DISPLAY(a)} IF(b > a){DISPLAY(b)}. Algo B: IF(a ≥ b){DISPLAY(a)} ELSE {DISPLAY(b)}. Fill in what each DISPLAYs for each input — watch the tie.
Complete the empty cells.
| Input (a, b) | Algo A displays | Algo B displays | Same behavior? |
|---|---|---|---|
| (88, 95) | 95 | Yes — both print 95 | |
| (95, 88) | 95 | 95 | Yes — both print 95 |
| (70, 70) | No — A prints nothing on a tie |
Watch out — “If Two Algorithms Look Alike, They Behave Alike”
Myth: When two algorithms are structured almost identically, you can assume they produce the same result and the same side effects.
Explain why this is wrong:
Stop and think
- In your own words, give the difference between an algorithm's RESULT and its SIDE EFFECT, using the higher-score example.
- Algorithm X computes the average and DISPLAYs it. Algorithm Y computes the same average but DISPLAYs each score first, then the average. Do they share a result? A side effect? Explain both answers.
- A classmate says 'these two loops look identical, so they must do the same thing.' State the one move that actually settles whether two algorithms behave the same, and name a boundary input worth testing.
Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.9 page.
02. Conditionals ⟷ Boolean Expressions
When an IF Is Just a Boolean in Disguise
- The CED says some conditional statements can be written as .
- IF(cond) { flag ← true } ELSE { flag ← false } is exactly equivalent to .
- The give-away pattern for this equivalence is a conditional whose only job is .
Four Lines That Are Really One
This IF/ELSE is equivalent to the single line passed ← (score ≥ 60). Same result, same side effect.
AP Pseudocode (what the exam tests)
score ← 72
IF(score ≥ 60) { passed ← true }
ELSE { passed ← false }
DISPLAY(passed)
Python (the runnable version)
score = 72
if score >= 60:
passed = True
else:
passed = False
print(passed)
Output
true
Run and edit this yourself in the coding exercises for this topic.
Collapse It, or Expand It
Conditional form
Boolean form
- To COLLAPSE a conditional into a Boolean expression (L.3) you .
- To EXPAND a Boolean expression into a conditional (L.4) you .
AP TIP: To expand a Boolean back to a conditional (L.4), wrap it: IF(expr) { x ← true } ELSE { x ← false }. To collapse a conditional to a Boolean (L.3), keep just the condition.
Match Each Conditional to Its Boolean
Each row is one equivalence. Fill in the missing equivalent form — the two forms must give the same result for every input.
Complete the empty cells.
| Conditional form | Equivalent Boolean expression |
|---|---|
| IF(score ≥ 60) { passed ← true } ELSE { passed ← false } | passed ← score ≥ 60 |
| IF(lives = 0) { gameOver ← true } ELSE { gameOver ← false } | |
| IF(coins ≥ 100) { win ← true } ELSE { win ← false } | win ← coins ≥ 100 |
| IF(NOT ready) { waiting ← true } ELSE { waiting ← false } |
Stop and think
- Collapse this conditional to a single Boolean expression: IF(health > 0) { alive ← true } ELSE { alive ← false }.
- Expand this Boolean expression into an equivalent IF/ELSE that stores true or false: eligible ← age ≥ 13.
- A classmate rewrites IF(x > 5) { y ← true } ELSE { y ← false } as y ← x > 5 and claims it is a DIFFERENT algorithm because it looks nothing alike. Explain why the two are equivalent — name the result and the side effect they share.
Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.9 page.
Today in one box
- Comparing algorithms means checking TWO things: same result (value computed) and same side effect (what is displayed/changed)
- Different algorithms can solve the same problem, and algorithms that LOOK alike can behave differently — trace boundary cases
- A conditional that only sets true/false collapses to an equivalent Boolean expression
- A Boolean expression expands back into an equivalent IF/ELSE
Before next class: Before tomorrow: you already wrote 'find the higher of two scores.' In one line, what would you change to make it find the LOWER score instead? Write it down.
Day 2: Building New from Old
Today’s objectives
- Create an algorithm from an idea, using the standard building blocks — max/min, sum/average, divisibility via MOD, and a robot's maze path (LO AAP-2.M)
- Combine and modify existing algorithms to solve a new problem, and explain how reusing correct building blocks reduces development time, testing, and errors (LO AAP-2.M)
Bell ringer
Here is a correct algorithm that finds the larger of two scores: IF(a > b) { result ← a } ELSE { result ← b }. Do NOT rewrite it from scratch. Change as little as possible to make it find the SMALLER score instead.
Write 1–3 sentences: What is the single change you made, and why did modifying the working algorithm beat starting over? What did you get to keep — the structure, the variable names, the tested shape?
01. The Building Blocks You Already Know
Knowing Existing Algorithms Helps You Build New Ones
- Says that knowledge of existing algorithms can help in .
- The four building blocks the CED names are max/min, sum/average, .
- You test whether one integer is evenly divisible by another using .
Largest and Smallest of Two Numbers
The same compare, flipped: > gives the max, < gives the min. With a=88, b=95 the max is 95 and the min is 88.
AP Pseudocode (what the exam tests)
a ← 88
b ← 95
IF(a > b) { largest ← a } ELSE { largest ← b }
IF(a < b) { smallest ← a } ELSE { smallest ← b }
DISPLAY(largest)
DISPLAY(smallest)
Python (the runnable version)
a = 88 b = 95 largest = a if a > b else b smallest = a if a < b else b print(largest) print(smallest)
Output
95 88
Run and edit this yourself in the coding exercises for this topic.
Total and Average of the Scores
Sum first, then divide by how many. Average = 270 / 3 = 90.
AP Pseudocode (what the exam tests)
a ← 80 b ← 90 c ← 100 sum ← a + b + c average ← sum / 3 DISPLAY(average)
Python (the runnable version)
a = 80 b = 90 c = 100 total = a + b + c average = total / 3 print(average)
Output
90
Run and edit this yourself in the coding exercises for this topic.
Does a Coin Appear on This Tile?
A coin sits on every 3rd tile. tile MOD 3 = 0 means 3 divides the tile number evenly.
AP Pseudocode (what the exam tests)
tile ← 12
IF(tile MOD 3 = 0)
{ DISPLAY("coin") }
ELSE
{ DISPLAY("no coin") }
Python (the runnable version)
tile = 12
if tile % 3 == 0:
print("coin")
else:
print("no coin")
Output
coin
Run and edit this yourself in the coding exercises for this topic.
Trace the Robot's Path Through the Maze
The hero starts at (1,1) facing East (Right). Run the commands in order and fill in the facing and position AFTER each command. On this grid, moving East increases the column; a right turn from East faces South (row increases when moving).
Complete the empty cells.
| Step | Command | Facing after | Position after (row, col) |
|---|---|---|---|
| start | — | East | (1, 1) |
| 1 | MOVE_FORWARD() | East | (1, 2) |
| 2 | MOVE_FORWARD() | East | |
| 3 | ROTATE_RIGHT() | (1, 3) | |
| 4 | MOVE_FORWARD() | South |
Stop and think
- Write the divisibility test that displays 'even' when a number n is even and 'odd' otherwise. Which building block did you reuse, and what did you change?
- The average building block 'contains' the sum building block. Explain what that means, and why computing an average is really two algorithms working together.
- The robot is at (2,3) facing South and executes ROTATE_LEFT() then MOVE_FORWARD(). Give its new facing and position, stating the rule for each command.
Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.9 page.
02. Create, Combine, and Modify
Three Ways to Make an Algorithm
- The three ways to create an algorithm are from an idea, by .
- Flipping a working find-max into find-min is an example of creating an algorithm by .
- Feeding a computed sum into a division to get an average is an example of .
Combine Blocks: Average, Then Pass/Fail
The average building block feeds a comparison block: compute the average, then decide pass or fail.
AP Pseudocode (what the exam tests)
s1 ← 80
s2 ← 90
s3 ← 100
avg ← (s1 + s2 + s3) / 3
IF(avg ≥ 60) { DISPLAY("pass") }
ELSE { DISPLAY("fail") }
Python (the runnable version)
s1, s2, s3 = 80, 90, 100
avg = (s1 + s2 + s3) / 3
if avg >= 60:
print("pass")
else:
print("fail")
Output
pass
Run and edit this yourself in the coding exercises for this topic.
Modify Blocks: From Max to Min
Original: find the MAX
Modified: find the MIN
- To modify find-max into find-min you change only .
- Modifying a correct algorithm shrinks your testing because .
AP TIP: Modifying keeps the structure and names, so you only re-check what you touched — far less than rewriting from scratch.
Why Build from Existing Blocks
- The three benefits of reusing correct building blocks are reducing development time, reducing testing, and .
- Errors in a program built from trusted blocks are most likely to be found .
Watch out — “Reusing Tested Blocks Means No Testing Needed”
Myth: If every building block was already tested and correct, the new program built from them needs no testing at all.
Explain why this is wrong:
Stop and think
- You need an algorithm that displays the average of three scores only if none of them is below 50. Name which building blocks you would COMBINE and describe the order you would connect them.
- Start from find-max of two numbers. MODIFY it so it displays 'tie' when the two numbers are equal and otherwise displays the larger. State every change you made.
- A classmate reuses a correct 'compute the average' block in a new program and says 'it was already tested, so I don't need to test my program.', explain what still must be tested and why.
Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.9 page.
Common AP Traps
Three ways Topic 3.9 loses points on the exam — one minute now, real points in May.
Result and side effect are not the same — in your own words:
Trace similar-looking code; don't eyeball it — in your own words:
Reuse reduces testing — it does not remove it — in your own words:
Developing Algorithms, in One Slide
- Compare algorithms on TWO axes — same result (value) and same side effect (what is displayed/changed); similar-looking code can still differ.
- A conditional that only stores true/false equals a Boolean expression, and back again.
- Create algorithms from an idea, by combining, or by modifying existing ones; know the building blocks — max/min, sum/average, divisibility via MOD, robot maze path.
- Reusing correct blocks reduces development time, testing, and errors — but the new combination still must be tested.
Exit check — I can…
- ☐ compare two algorithms for the same result and the same side effect, and explain why similar-looking algorithms can differ (LO AAP-2.L)
- ☐ rewrite a conditional as an equivalent Boolean expression and back again (LO AAP-2.L)
- ☐ create an algorithm from an idea using the standard building blocks — max/min, sum/average, divisibility via MOD, and a robot's maze path (LO AAP-2.M)
- ☐ combine and modify existing algorithms, and explain how reusing correct blocks reduces development time, testing, and errors (LO AAP-2.M)
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]