{"base":{"id":"cs8-06","subject":"Algorithms","title":"Tracing and predicting","scene":"code","steps":[{"mode":"trace","eyebrow":"Trace tables","title":"Follow it line by line","body":"To trace an algorithm, you follow it line by line and record every change in a trace table. This loop adds up the numbers from 1 to n. Step through and watch the table grow.","controls":[{"id":"n","label":"Input n","min":1,"max":6,"step":1,"value":4}],"actions":[{"id":"step","label":"Run one line"},{"id":"run","label":"Run it all"}]},{"mode":"predict","eyebrow":"Predict first","title":"What will it output?","body":"Programmers predict the output before running a program. This one starts with 3 and doubles it three times. Choose your prediction, then run it and compare.","actions":[{"id":"g0","label":"9"},{"id":"g1","label":"18"},{"id":"g2","label":"24"},{"id":"run","label":"Run to check"}],"start":3,"guesses":[9,18,24]},{"mode":"predict","eyebrow":"Different inputs","title":"Change the input, trace again","body":"One input tests one case. Try a small start value, then a bigger one. Predict each output in your head first, then run it and check the trace table.","controls":[{"id":"start","label":"Input start","min":1,"max":10,"step":1,"value":1}],"actions":[{"id":"step","label":"Run one line"},{"id":"run","label":"Run it all"}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Trace it","body":"The program is: INPUT a, then b ← a − 2, then a ← b × 2, then OUTPUT a. The input is 5.","question":"What is output?","options":["6","5","3"],"answer":0,"right":"Correct. b = 5 − 2 = 3, then a = 3 × 2 = 6. The old value of a is replaced.","wrong":"Not quite. Trace each line in order and remember a gets a new value.","look":"trace"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"Tracing is only for finding bugs.\"","body":"Tracing is also how programmers understand code and predict what it will do."},{"tag":"Popular idea","title":"\"If my prediction was wrong, the program must be wrong.\"","body":"Sometimes the prediction is the mistake. The trace shows which one went wrong."},{"tag":"Popular idea","title":"\"You write a new row in the trace table for every line.\"","body":"Write a row when a variable changes or something is output. Lines that change nothing don't need one."}]},{"type":"cards","id":"worked","eyebrow":"Worked example","title":"A full trace","items":[{"tag":"Program","title":"Add up 1 to n","body":"INPUT n\ntotal ← 0\nFOR i ← 1 TO n\n    total ← total + i\nNEXT i\nOUTPUT total","code":true},{"tag":"Trace","title":"n = 3","body":"n  i  total  output\n3\n       0\n   1   1\n   2   3\n   3   6\n             6","code":true},{"tag":"Tip","title":"Start small","body":"Trace with a tiny input first (n = 1 or 2). Each step is easy to check, and the pattern shows before the numbers get big."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Paper computer","items":[{"title":"Trace a game score","time":"15 minutes","need":["Paper and a pen","A ruler"],"steps":["Copy this: score ← 0, then REPEAT 4 TIMES: score ← score + 5, then OUTPUT score.","Draw a trace table with columns 'score' and 'output'.","Predict the output first and write it in a corner.","Trace it row by row. Was your prediction right? Now change + 5 to × 2 and start score at 1. Predict, then trace again."],"why":"Writing each value down stops you losing track, which is exactly why programmers use trace tables.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"What is a trace table for?","options":["Drawing flowcharts","Typing code faster","Recording how variables change"],"answer":2,"right":"Correct. Each row shows a change.","wrong":"Not quite. Think about what you write in it."},{"question":"Why predict before running a program?","options":["To check you understand it","Programs run faster","It is the law"],"answer":0,"right":"Correct. Comparing prediction and result tests your understanding.","wrong":"Not quite. What does comparing tell you?"},{"question":"total ← 0, then total ← total + 4 twice. What is total?","options":["4","8","0"],"answer":1,"right":"Correct. 0 + 4 = 4, then 4 + 4 = 8.","wrong":"Not quite. Add 4 two times."},{"question":"FOR i ← 1 TO 3 repeats the inside line how many times?","options":["1","4","3"],"answer":2,"right":"Correct. i takes the values 1, 2 and 3.","wrong":"Not quite. Count the values of i."},{"question":"Why test with more than one input?","options":["One input is always enough","Different inputs can reveal different mistakes","To fill the screen"],"answer":1,"right":"Correct. A program can work for one value and fail for another.","wrong":"Not quite. Could a program work for 3 but not for 0?"},{"question":"value ← 2, then REPEAT 2 TIMES value ← value × 3. What is value?","options":["12","8","18"],"answer":2,"right":"Correct. 2 × 3 = 6, then 6 × 3 = 18.","wrong":"Not quite. Multiply by 3 twice."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["what tracing an algorithm means","how to fill in a trace table","how a FOR loop repeats steps","why we predict before running a program","why testing with different inputs matters"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"trace","meaning":"Follow an algorithm line by line, by hand."},{"word":"trace table","meaning":"A table showing each value the variables take."},{"word":"predict","meaning":"Work out what will happen before it happens."},{"word":"loop","meaning":"Steps that repeat, such as FOR … NEXT or REPEAT."},{"word":"bug","meaning":"A mistake in an algorithm or program."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize GCSE Computer Science: Trace tables"]}]},"overlay":null}