Computer Science and Digital Literacy · 35 minutes
Suggested grades 9–12 · Unit 1: Algorithms and decision logic · Lesson 1 of 2
Trace an Algorithm with an Exception
Follow a written loop precisely and test ordinary, empty and invalid inputs.
Mr. S Teaches · Original starter lesson · HS-CS-01 · Grade range is planning guidance.Watch, notice, discuss
Video choices for this lesson
Preview a video, then choose what fits your learners. “Show to class” opens a larger view; use the player’s play and full-screen controls when you’re ready.
Teacher preview needed. Check the language, level and length before showing. YouTube may show ads or related videos.
Programming Basics: Statements & Functions: Crash Course Computer Science #12
By CrashCourse
Review assignment, conditions and loops before tracing the filtered sum by hand.
Open on YouTube: Programming Basics: Statements & Functions: Crash Course Computer Science #12 (opens a new tab) ↗Intro to Algorithms: Crash Course Computer Science #13
By CrashCourse
Provide broader algorithm context, then trace the lesson’s specific list procedure rather than the video’s sorting examples.
Open on YouTube: Intro to Algorithms: Crash Course Computer Science #13 (opens a new tab) ↗Where does the running total change, and which step incorrectly resets it?
An external player loads only after you choose Preview or Show to class. Playback connects to YouTube. Videos belong to their creators; availability and embedding can change.
Learning objective
Produce a correct trace table and locate a reset bug in a list-processing procedure.
Before you begin
- Basic arithmetic
- Reading a table
Gather your materials
- Paper and pencil
- A trace table with entry,condition and total columns
Language to know
- algorithm
- A finite, explicit procedure for solving a class of problems.
- state
- The current values of a procedure’s variables.
- trace
- A recorded step-by-step execution using stated inputs.
Understand the concept
Procedure: Begin with total=0. Read each list entry in order. If an entry is not a number, stop and report “invalid input.” If it is a number greater than 0, add it to total; otherwise leave total unchanged. After all entries, report total. An empty list reports 0. Tracing records what actually happens, not what a reader assumes the procedure intended. Moving the initialization inside the loop changes the algorithm.
Teach it step by step
- Read the procedure and mark initialization, repeated steps, selection and the invalid-input exception.
- Trace[3,−2,5,0], recording entry, condition result and total after each step.
- Test the empty list and[4,“?”,2], applying the stated exceptions exactly.
- Move total=0 inside the loop in a hypothetical faulty version and explain how the trace reveals the bug.
Make the thinking visible
Worked example or model response
The valid trace totals are 3,3,8,8, so output 8. The empty input outputs 0. Input[4,“?”,2] stops at“?” and reports invalid input rather than a partial sum. Resetting inside each iteration loses earlier contributions.
Guided practice
Trace[−1,2,2,−3,7] and explain each unchanged state.
Independent practice
1. Trace[0,−5]. 2. Give an input distinguishing the correct algorithm from the reset-inside-loop version. 3. Explain why silently treating“?” as 0 violates the procedure.
Quick assessment
Which line ensures the empty-list behavior is defined?
Answer key & assessment criteria
Answers and teaching guidance
- Guided: Totals 0,2,4,4,11; nonpositive values do not change the total.
- Independent 1: Output 0. 2:[2,3] yields 5 correctly and 3 with the reset bug. 3: The exception requires stopping with an invalid-input report.
- Quick check: Initialization sets total=0 before iteration, so a list with no entries still has a defined result.
Look for: Require the learner to explain the interpretation and check, not only state a result. Assess the written trace or evidence record; do not request real passwords, accounts or personal data.
Make it work for your learner
More support
Use paper variable cards and update one entry per row before discussing the bug.
Extension challenge
Modify the written procedure to return both sum and count of positive entries, specifying every changed step.
Verified references · Selected contributions
Standards connections
Subject-content alignment not yet verified
Supporting literacy connections
These records apply to the specified reading, writing or communication task. They do not establish subject-content alignment.
Supporting Common Core connection
RST.9-10.3
Common Core · Grades 9–10
Follow a multistep technical procedure and its stated empty/invalid-input cases.
Read the official standard ↗Supporting grades 9–10 technical-literacy practice in the stated task, not a verified computer-science content alignment or full text-complexity assessment.
Verified 2026-11-04. Selected contribution, not full attainment.
