{"base":{"id":"cs9-03","subject":"Algorithms","title":"Predicting what a loop outputs","scene":"code9","steps":[{"mode":"predict","eyebrow":"Predict, then run","title":"What will it print?","body":"Good programmers predict the output before running code. Choose your prediction for this loop, then run it to check.","actions":[{"id":"p0","label":"1 4 9 16"},{"id":"p1","label":"1 2 3 4"},{"id":"p2","label":"2 4 6 8"},{"id":"run","label":"Run"}]},{"mode":"startval","eyebrow":"Starting values","title":"Change the start","body":"x doubles on each pass. Change the starting value of x and predict the last output before you run it.","controls":[{"id":"s","label":"Start value","min":1,"max":5,"step":1,"value":1}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"offbyone","eyebrow":"Off-by-one","title":"One too many, one too few","body":"We want exactly 5 stars. Try three loops: which one gives 5? Checking the first and last pass catches most loop mistakes.","actions":[{"id":"a0","label":"1 TO 5"},{"id":"a1","label":"0 TO 5"},{"id":"a2","label":"1 TO 4"},{"id":"run","label":"Run"}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Predict","body":"x ← 2\nFOR i ← 1 TO 3\n  x ← x + 5\nNEXT i\nOUTPUT x","question":"What is output?","options":["17","7","15"],"answer":0,"right":"Correct. 2 → 7 → 12 → 17.","wrong":"Not quite. Add 5 three times.","look":"startval"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"FOR i ← 0 TO 5 runs 5 times.\"","body":"It runs 6 times: 0, 1, 2, 3, 4, 5."},{"tag":"Popular idea","title":"\"The output appears all at once at the end.\"","body":"An OUTPUT inside the loop prints on every pass."},{"tag":"Popular idea","title":"\"Changing the start value only changes the first output.\"","body":"Every later value depends on it."}]},{"type":"cards","id":"code","eyebrow":"Pseudocode","title":"Predict these","items":[{"tag":"Squares","title":"Output","body":"FOR i ← 1 TO 4\n    OUTPUT i × i\nNEXT i\n→ 1 4 9 16","code":true},{"tag":"Running total","title":"Output","body":"t ← 0\nFOR i ← 1 TO 3\n    t ← t + i\n    OUTPUT t\nNEXT i\n→ 1 3 6","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Prediction game","items":[{"title":"Swap loops","time":"15 minutes","need":["Paper","A partner"],"steps":["Each write a short loop (3–5 lines) that outputs numbers.","Swap and predict the full output in writing.","Trace together to check.","Score a point for every fully correct prediction."],"why":"Predicting forces you to think through every pass, which is exactly what tracing is.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"FOR i ← 1 TO 3: OUTPUT i × 2. Output?","options":["2 4 6","1 2 3","3 6 9"],"answer":0,"right":"Correct. 1×2, 2×2, 3×2.","wrong":"Not quite. Double each i."},{"question":"FOR i ← 5 TO 7: OUTPUT i. How many outputs?","options":["7","3","2"],"answer":1,"right":"Correct. 5, 6, 7.","wrong":"Not quite. Count the values."},{"question":"x ← 1; FOR i ← 1 TO 4: x ← x × 3. Final x?","options":["12","27","81"],"answer":2,"right":"Correct. 1 → 3 → 9 → 27 → 81.","wrong":"Not quite. Multiply by 3 four times."},{"question":"Which loop outputs exactly 10 lines?","options":["FOR i ← 0 TO 10","FOR i ← 1 TO 10","FOR i ← 1 TO 9"],"answer":1,"right":"Correct. 1 to 10 is 10 values.","wrong":"Not quite. Count carefully."},{"question":"An extra pass would change…","options":["nothing","only the first output","the last output"],"answer":2,"right":"Correct. One more pass adds one more output.","wrong":"Not quite. What happens at the end?"},{"question":"Why predict before running?","options":["It's faster than running","Computers need it","To check your understanding"],"answer":2,"right":"Correct. Mismatches show what you misunderstood.","wrong":"Not quite. What does a wrong prediction tell you?"}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["how to predict a loop's output pass by pass","how the starting value affects every output","what an off-by-one error is","why checking the first and last pass helps"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"output","meaning":"Data a program produces."},{"word":"pass","meaning":"One run through the loop body."},{"word":"off-by-one error","meaning":"A loop that runs one time too many or too few."},{"word":"initial value","meaning":"The starting value of a variable."},{"word":"predict","meaning":"Say what will happen before it does."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computer Science","Raspberry Pi Foundation, Computing curriculum resources"]}]},"overlay":null}