AP Computer Science Principles · Unit 3 lecture · Part 14 of 14 · 9 min

Unit 3 wrap-upPut it together

Mr. Thompkins
That was the biggest unit of the course, and you made it. Let's pull it into one picture, then connect it to the Create task, where Unit 3 is worth most of your written-response points.

Everything in this unit builds on the three ingredients. Sequencing runs assignments and expressions in order (3.1 to 3.4). Selection uses Booleans to choose a path (3.5 to 3.7). Iteration repeats, and together they build algorithms (3.8, 3.9). Lists give many values one name, and traversals and searches work through them (3.2, 3.10, 3.11). Procedures package all of it under names, with parameters and return values, and libraries share it (3.12 to 3.14). Random values and simulations model the world (3.15, 3.16). And efficiency and decidability tell you which problems are practical, or possible (3.17, 3.18).

The Create task and Unit 3

Your Create task program must include a list that stores data and is then used to fulfill the program's purpose, and a procedure you wrote, with at least one parameter that affects what it does, containing an algorithm with sequencing, selection and iteration. Written response 2 asks about exactly these, in your own code.

PromptUnit 3 idea it testsWhat earns the point
2(a) Algorithm developmentIteration, selection, sequencingExact passes, the exact condition, a precise change
2(b) Errors and testingProcedure calls, conditionalsTwo calls that run different code, with exact results
2(c) Data and procedural abstractionLists, procedures, parametersHow it manages complexity, and the code you would need without it
The three parts of written response 2, and the Unit 3 ideas behind them.
?Wait, what's in the Personalized Project Reference again?
Screen captures of your own code: the procedure definition, a place where it's called, the code where your list is stored, and the code where it's used. No comments, and nothing from course materials. You use it to answer the written responses on exam day.
?Wait, how do I count loop passes for a written response?
For FOR EACH, one pass per element of the list in that call. For REPEAT n TIMES, exactly n. For REPEAT UNTIL, trace it: count the passes before the first check that comes out true.
Pause and think
Your Create task procedure has an IF and an ELSE inside a FOR EACH loop. What would make a strong answer to "describe two calls that cause different code segments to run"?
I thought about it. Show me
Two real calls with specific arguments: one where the IF condition is true for the elements (so the IF block runs), and one where it's false (so the ELSE block runs), each with the exact value returned or displayed. Name the variables and values from your actual code.

Traps that cost points

✕ Counting list indexes or string positions from 0.
✓ On the exam, aList[1] and position 1 are the first.
✕ Using an old value after a variable changed.
✓ Trace line by line; each line uses the current values.
✕ Doing + before MOD.
✓ MOD has the same priority as * and /.
✕ Using OR for a range.
✓ Inside a range: AND. Outside: OR.
✕ Negating > as <.
✓ The opposite of > is ≤; of < is ≥.
✕ Thinking REPEAT UNTIL runs once before checking.
✓ It checks first, every pass, and can run zero times.
✕ Thinking INSERT replaces an element.
✓ INSERT shifts later elements right; assignment replaces.
✕ Running binary search on an unsorted list.
✓ Binary search needs sorted data. Otherwise use linear search.
✕ Thinking code after RETURN still runs.
✓ RETURN ends the procedure immediately.
✕ Saying RANDOM(1, 6) never gives 6.
✓ Both ends are included.
✕ Turning the robot from your own point of view.
✓ Face the way the robot faces.
✕ Calling a slow problem undecidable.
✓ Slow but solvable: use a heuristic. Undecidable: no algorithm works for all inputs.

The reference sheet, decoded

x ← expression
AssignmentEvaluate the right side, then store it. The old value is gone.
a MOD b
RemainderSame priority as * and /.
=, ≠, >, <, ≥, ≤ / NOT, AND, OR
BooleansRange: AND. Outside: OR. Negate: flip each part and swap AND/OR.
IF (c) { } ELSE { }
SelectionExactly one block runs.
REPEAT n TIMES { }
Counted loopExactly n passes.
REPEAT UNTIL (c) { }
Conditional loopCheck first; stop at the first true.
FOR EACH item IN aList { }
TraversalOne pass per element, in order.
aList[i], INSERT, APPEND, REMOVE, LENGTH
ListsIndexes 1 to LENGTH. INSERT and REMOVE shift.
PROCEDURE f(p) { RETURN(x) }
ProcedureCall it with arguments; RETURN ends it.
RANDOM(a, b)
Random integera to b inclusive: b - a + 1 values.
MOVE_FORWARD(), ROTATE_LEFT(), ROTATE_RIGHT(), CAN_MOVE(d)
RobotDirections from the robot's view.

Strategy for Unit 3 questions

  1. Trace on paper: one column per variable, one row per line or per loop check. Never in your head.
  2. For lists, rewrite the whole list after every INSERT, APPEND, REMOVE or assignment.
  3. For loops, decide first: counted (REPEAT n TIMES, FOR EACH) or conditional (REPEAT UNTIL, check first).
  4. For "which code works" questions, test each choice with a boundary value and a tie; eliminate any that fail.
  5. For multiple-select questions (choose 2), judge each choice separately as true or false.
  6. For the written responses, use real names, real arguments and exact values from your own code.
Mixed practice: one procedure, every skill
This procedure is meant to return how many scores in the list are at least goal. (a) What does countAtLeast([70, 85, 60, 90], 80) return? (b) How many times does the loop body run in that call? (c) Give a call that runs the IF block and one that never runs it, with results. (d) Explain how the list and the parameter goal manage complexity.
1PROCEDURE countAtLeast(scores, goal)2{3  count ← 04  FOR EACH s IN scores5  {6    IF (s ≥ goal)7    {8      count ← count + 19    }10  }11  RETURN(count)12}
A procedure with a list, a parameter, selection and iteration: everything the Create task asks for.
  1. (a) 85 and 90 are at least 80, so it returns 2.
  2. (b) FOR EACH runs once per element: 4 times.
    Why?
    Exact counts earn the point on written response 2(a).
  3. (c) countAtLeast([95], 90) runs the IF block (95 ≥ 90) and returns 1. countAtLeast([50], 90) never runs it and returns 0.
    Why?
    Real calls, the code each runs, exact results: that's what 2(b) asks for.
  4. (d) The list holds any number of scores under one name, so one loop handles them all; goal lets the same code count for any cutoff instead of writing a separate procedure for 70, 80 and 90.
    Why?
    For 2(c), say what the abstraction does and what you'd need without it.

Answer: (a) 2 (b) 4 times (c) countAtLeast([95], 90) returns 1; countAtLeast([50], 90) returns 0 (d) One list and one loop for any number of scores; one parameter for any cutoff.

Stuck? Get a hint
  1. Which scores are at least 80?
  2. FOR EACH runs once per element of the list in that call.
  3. Pick one score above the goal and one below it.
Check yourself
What is displayed when this code segment runs?
vals ← [4, 7, 1, 8]
total ← 0
i ← LENGTH(vals)
REPEAT UNTIL (i < 1)
{
  IF (vals[i] MOD 2 = 0)
  {
    total ← total + vals[i]
  }
  i ← i - 1
}
DISPLAY(total)
  1. A20
  2. B12
  3. C8
  4. D4
Show the answer

B. The loop walks backward from index 4 to index 1 and adds the even values: 8, then 4. The total is 12. 20 adds every value; the loop stops correctly once i reaches 0, so no index error occurs.

Check yourself
Which TWO statements are true? I. Binary search can be used on any list. II. A procedure's parameters receive the values of the arguments when it is called. III. REPEAT UNTIL always runs its body at least once. IV. RANDOM(2, 5) can return 2, 3, 4 or 5.
  1. AII and IV
  2. BI and II
  3. CI and III
  4. DIII and IV
Show the answer

A. II and IV are true: arguments set the parameters, and RANDOM includes both ends. I is false (binary search needs sorted data) and III is false (REPEAT UNTIL checks first and can run zero times).

Mr. Thompkins
That's Big Idea 3, a third of the exam. Take the Unit 3 quiz, then watch the five video classes if you haven't, and practice the two FRQ walkthroughs with your own Create task code. Trace on paper, every time. You've got this.