{"base":{"id":"cs9-02","subject":"Algorithms","title":"Following a loop","scene":"code9","steps":[{"mode":"forloop","eyebrow":"Counter loops","title":"FOR … NEXT","body":"A FOR loop repeats the steps inside it a fixed number of times. The counter i goes up by 1 each pass. Run it and watch i and total change in the trace table. Change n.","controls":[{"id":"n","label":"n","min":1,"max":6,"step":1,"value":4}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"whileloop","eyebrow":"Condition loops","title":"WHILE … ENDWHILE","body":"A WHILE loop checks its condition before each pass and stops when it becomes false. Run the countdown and note when the condition is checked.","controls":[{"id":"n","label":"Start count","min":1,"max":5,"step":1,"value":3}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"repeat","eyebrow":"Inside or outside?","title":"What repeats and what runs once","body":"Only the lines inside the loop repeat. Lines before it run once at the start; lines after it run once at the end. Run it and count.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Count the passes","body":"FOR i ← 1 TO 6\n  OUTPUT \"Hi\"\nNEXT i","question":"How many times is \"Hi\" output?","options":["5","6","7"],"answer":1,"right":"Correct. i takes the values 1, 2, 3, 4, 5 and 6.","wrong":"Not quite. List the values of i.","look":"forloop"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"A loop always runs at least once.\"","body":"A WHILE loop whose condition is false at the start runs zero times."},{"tag":"Popular idea","title":"\"The counter keeps its last value + 1 after a FOR loop.\"","body":"It depends on the language; never rely on it in pseudocode."},{"tag":"Popular idea","title":"\"NEXT i adds 2 each time.\"","body":"It adds 1 unless a different STEP is given."}]},{"type":"cards","id":"code","eyebrow":"Pseudocode","title":"Loop patterns","items":[{"tag":"FOR","title":"Fixed count","body":"FOR i ← 1 TO 5\n    OUTPUT i\nNEXT i","code":true},{"tag":"WHILE","title":"Until a condition fails","body":"x ← 1\nWHILE x < 100\n    x ← x × 2\nENDWHILE\nOUTPUT x","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Human loop","items":[{"title":"Act out a loop","time":"10 minutes","need":["A partner","Paper"],"steps":["Write: total ← 0; FOR i ← 1 TO 5: total ← total + i; NEXT i.","One person is 'i' and holds up fingers; the other is 'total' and writes the value.","Act out each pass, filling in a trace table.","Change TO 5 to TO 3. What changes?"],"why":"Acting out each pass makes it clear which values change and when.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"FOR i ← 3 TO 5 runs how many times?","options":["5","2","3"],"answer":2,"right":"Correct. i = 3, 4, 5.","wrong":"Not quite. Count the values."},{"question":"When does a WHILE loop check its condition?","options":["Before each pass","Only at the end","Never"],"answer":0,"right":"Correct. If it is false, the loop stops.","wrong":"Not quite. Check, then run."},{"question":"total ← 0; FOR i ← 1 TO 3: total ← total + 2. Final total?","options":["6","2","3"],"answer":0,"right":"Correct. 2 added three times.","wrong":"Not quite. How many passes?"},{"question":"x ← 10; WHILE x > 0: x ← x − 4. How many passes?","options":["2","4","3"],"answer":2,"right":"Correct. 10 → 6 → 2 → −2.","wrong":"Not quite. Trace x."},{"question":"A line after NEXT i runs…","options":["every pass","never","once"],"answer":2,"right":"Correct. It is outside the loop.","wrong":"Not quite. Is it inside the loop?"},{"question":"What is a trace table for?","options":["Drawing flowcharts","Recording values as an algorithm runs","Storing files"],"answer":1,"right":"Correct. Each row is a step.","wrong":"Not quite. Tracking variables."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["how a FOR loop counts","how a WHILE loop decides when to stop","which statements repeat and which run once","how to record a loop in a trace table"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"loop","meaning":"Steps that repeat."},{"word":"iteration","meaning":"Repeating steps; also one pass of a loop."},{"word":"counter","meaning":"A variable that counts passes."},{"word":"condition","meaning":"A true/false test."},{"word":"trace table","meaning":"A table showing values at each step."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computer Science","Raspberry Pi Foundation, Computing curriculum resources"]}]},"overlay":null}