{"base":{"id":"cs10-10","subject":"Algorithms","title":"Tracing a program","scene":"code10","steps":[{"mode":"trace","eyebrow":"Trace tables","title":"Be the computer","body":"A trace table records the value of every variable after each line runs. It is how programmers check an algorithm by hand. Step through and fill the table in your head first.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"whiletr","eyebrow":"Tracing a WHILE loop","title":"When does it stop?","body":"With a WHILE loop, the condition is checked before every repeat. Trace it to find how many times it runs and what it outputs. Predict before you press Run.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"bug","eyebrow":"Finding errors","title":"Runs fine, wrong answer","body":"This program should add the numbers 1 to 5 and print 15. It runs without crashing, but prints the wrong total: a logic error. Trace it to find why, then fix it.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"fix","label":"Fix the code"},{"id":"reset","label":"Reset"}]},{"mode":"testdata","eyebrow":"Test data","title":"Try to break it","body":"A good tester tries every kind of input: normal, boundary, erroneous and the wrong type. For a mark that must be 0 to 100, tap each kind and see what should happen.","actions":[{"id":"t0","label":"Normal"},{"id":"t1","label":"Boundary"},{"id":"t2","label":"Erroneous"},{"id":"t3","label":"Wrong type"}]},{"mode":"quiz","look":"testdata","eyebrow":"Check yourself","title":"Trace it","body":"x = 2\nfor i in range(3):\n    x = x * 2\nprint(x)","question":"What is printed?","options":["16","8","6"],"answer":0,"right":"Correct. The loop runs 3 times: 2 → 4 → 8 → 16.","wrong":"Not quite. range(3) gives 0, 1, 2: three repeats. Double x each time."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Traces written out","intro":"Draw one column per variable, plus one for output. Add a new row each time a value changes.","items":[{"tag":"Trace","title":"Total of 3, 5, 8","body":"Total ← 0\nFOR EACH N IN [3, 5, 8]\n  Total ← Total + N\nNEXT\n\n N | Total\n---+------\n   |   0\n 3 |   3\n 5 |   8\n 8 |  16\noutput 16","code":true},{"tag":"Trace","title":"A countdown","body":"C ← 3\nWHILE C > 0\n  OUTPUT C\n  C ← C − 1\nENDWHILE\nOUTPUT \"Go!\"\n\n C | output\n 3 | 3\n 2 | 2\n 1 | 1\n 0 | Go!","code":true},{"tag":"Error types","title":"Syntax vs logic vs runtime","body":"syntax:   prnt(\"hi\")       won't run at all\nruntime:  print(10 / 0)     crashes while running\nlogic:    average = a + b / 2\n          runs, but wrong answer\n          (should be (a + b) / 2)","code":true},{"tag":"Test plan","title":"A test plan for 'age 13–19'","body":"test | type      | expected\n 15  | normal    | accept\n 13  | boundary  | accept\n 19  | boundary  | accept\n 12  | erroneous | reject\n 20  | erroneous | reject\n\"ab\" | wrong type| reject","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Writing the final value only","body":"A trace table shows every change, row by row. Skipping rows hides exactly where an error happens."},{"tag":"Mistake","title":"Forgetting the last check of a WHILE loop","body":"The condition is tested one final time, when it becomes False. That last check is what stops the loop."},{"tag":"Mistake","title":"Testing only normal data","body":"Programs usually fail at the edges. Always include boundary values and data that should be rejected."},{"tag":"Mistake","title":"Confusing erroneous and boundary data","body":"100 is boundary data for 0–100 (allowed, at the edge). 101 is erroneous (just outside)."}]},{"type":"cards","id":"method","eyebrow":"Set out your answer","title":"Getting full marks on traces","items":[{"tag":"1 · Set up columns","title":"Every variable and the output","body":"Use the column headings given. If none are given, list variables in the order they appear."},{"tag":"2 · One row per change","title":"Read the code slowly","body":"Only write a value when it changes. Leave other cells blank on that row."},{"tag":"3 · Explain the error","title":"Say what and how to fix","body":"\"Line 3 uses range(1, 5) so 5 is never added. Change it to range(1, 6).\""}]},{"type":"bars","id":"bugs","eyebrow":"Data","title":"When is a bug cheapest to fix?","intro":"Software engineers find that the later a bug is found, the more it costs to fix (rough relative costs).","items":[{"label":"While designing (trace tables, dry runs)","value":5,"display":"× 1","color":"#1f9d55"},{"label":"While coding","value":15,"display":"× 5","color":"#0e9aa7"},{"label":"During testing","value":40,"display":"× 10–20","color":"#d9a020"},{"label":"After release to users","value":100,"display":"× 50–100+","color":"#e0463e"}],"facts":[{"title":"The first 'bug'","body":"In 1947 engineers working with Grace Hopper found a moth stuck in a relay of the Harvard Mark II computer and taped it in their logbook: 'first actual case of bug being found'. The word 'bug' was already used for faults, but the moth made it famous."},{"title":"Dry run","body":"Tracing an algorithm on paper, without a computer, is called a dry run. Professionals still do it for tricky code."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Tools","title":"Debuggers trace for you","body":"Programming tools (IDEs) have a debugger: set a breakpoint, run line by line and watch variables change. It's an automatic trace table. You'll use one in Term 2."},{"tag":"Real world","title":"Testing teams","body":"Large companies employ testers whose whole job is to break software: typing silly inputs, clicking things in the wrong order, using huge files. Every bug they find is one users never see."},{"tag":"Careers","title":"Who traces and tests?","body":"Software testers, quality-assurance engineers, security researchers hunting for weaknesses, and every programmer fixing their own code."},{"tag":"Coming next","title":"Where this leads","body":"Term 2 looks inside the computer: the processor that actually runs these instructions, memory, storage and networks."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one before opening the card.","items":[{"tag":"Challenge","title":"Trace: a = 10, b = 3. WHILE a >= b: a ← a − b, c ← c + 1 (c starts at 0). Output a and c. What does this algorithm do?","body":" a | b | c\n10 | 3 | 0\n 7 |   | 1\n 4 |   | 2\n 1 |   | 3\noutput: a = 1, c = 3\nIt divides by repeated subtraction: c is 10 DIV 3 and a is 10 MOD 3.","code":true},{"tag":"Challenge","title":"Find the logic error: IF Mark > 50 THEN OUTPUT \"Pass\" ELSE OUTPUT \"Fail\" (pass mark 50)","body":"A mark of exactly 50 outputs \"Fail\" but should pass.\nFix: IF Mark >= 50 THEN …\nThe boundary test (50) is the one that catches it."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Bug hunting","items":[{"title":"Human computer","time":"20 minutes","need":["A friend","Paper","The pseudocode cards on this page"],"steps":["One person reads a short algorithm aloud, line by line.","The other acts as memory: writes each variable on a sticky note and updates it.","Keep a trace table on paper as you go.","Swap roles with a new algorithm."],"why":"Acting it out slowly is exactly what a computer does, one instruction at a time, and shows how values change.","safety":"No safety risks."},{"title":"Write a test plan","time":"15 minutes","need":["Paper"],"steps":["Choose a rule, for example 'a password must be 8 to 20 characters'.","Write normal, boundary, erroneous and extreme test cases.","Write the expected result for each.","If you can code, write the check in Python and run your tests."],"why":"A written test plan makes sure you test every kind of input, not just the obvious ones.","safety":"Don't use real passwords as test data."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"What does a trace table record?","options":["Variable values as each line runs","The speed of the program","The program's file size"],"answer":0,"right":"Correct. Row by row, every change.","wrong":"Not quite. It tracks values while tracing."},{"question":"A program runs but gives the wrong answer. What kind of error is it?","options":["Syntax error","Logic error","Hardware error"],"answer":1,"right":"Correct. The code is valid but the method is wrong.","wrong":"Not quite. A syntax error would stop it running."},{"question":"Which is a syntax error?","options":["total = a + b / 2 (should be (a+b)/2)","Dividing by zero","pirnt(\"Hello\")"],"answer":2,"right":"Correct. The misspelled keyword breaks the rules of the language.","wrong":"Not quite. Which one breaks the grammar of the language?"},{"question":"For 'age must be 0 to 120', which is boundary data?","options":["120","50","−3"],"answer":0,"right":"Correct. It's at the very edge of the allowed range.","wrong":"Not quite. Boundary data sits exactly at the edge."},{"question":"C ← 1; WHILE C < 10: C ← C * 2. What is C at the end?","options":["8","16","10"],"answer":1,"right":"Correct. 1, 2, 4, 8, 16: 16 is not < 10, so it stops.","wrong":"Not quite. Keep doubling until C is no longer below 10."},{"question":"What is a dry run?","options":["Running code with no data","Testing on a new computer","Tracing an algorithm by hand"],"answer":2,"right":"Correct. Paper and pencil, no computer.","wrong":"Not quite. It doesn't use a computer at all."},{"question":"For 'mark 0 to 100', which is erroneous data?","options":["−1","0","100"],"answer":0,"right":"Correct. It's outside the range and should be rejected.","wrong":"Not quite. Erroneous data is outside the allowed range."},{"question":"total = 0; for i in range(1, 5): total += i. A programmer wanted 1+2+3+4+5. The fix is…","options":["total = 1","range(1, 6)","range(0, 5)"],"answer":1,"right":"Correct. range stops before its end value.","wrong":"Not quite. Which number never gets added?"}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["complete a trace table for a sequence of assignments","trace a FOR loop and a WHILE loop","work out the output of an algorithm by hand","explain what an algorithm does from its trace","tell syntax, runtime and logic errors apart","find and fix a logic error using a trace","choose normal, boundary, erroneous and wrong-type test data","write a test plan with expected results"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"trace table","meaning":"A table showing variable values as an algorithm runs."},{"word":"dry run","meaning":"Tracing an algorithm by hand, without a computer."},{"word":"syntax error","meaning":"Code that breaks the rules of the language, so it won't run."},{"word":"logic error","meaning":"Code that runs but gives the wrong result."},{"word":"runtime error","meaning":"An error that crashes the program while it runs."},{"word":"normal data","meaning":"Typical input that should be accepted."},{"word":"boundary data","meaning":"Input at the edge of what is allowed."},{"word":"erroneous data","meaning":"Input that should be rejected."},{"word":"test plan","meaning":"A list of tests with their data and expected results."},{"word":"debugger","meaning":"A tool that runs code step by step to find errors."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize, GCSE Computer Science: Trace tables and testing","Smithsonian National Museum of American History: the 1947 'first bug' logbook","Isaac Computer Science: Testing and errors","IBM Systems Sciences Institute (widely quoted): relative cost of fixing defects"]}]},"overlay":null}