Idea 15of 23Builds onA question inside a questionTrace code in a table
A loop with an IF inside
What happens when a loop asks a question on every pass?
Today you'll discover
- how the IF is asked again each pass
- why the answer can change
- how to trace it
Think firstSET 3, then 4 passes of: IF number < 10 …
The IF is inside the loop. What does the code output?
Decide first, then press Next. Ask the IF's question again on every pass.
Code: SET 3, then REPEAT 4 TIMES with IF number < 10 THEN MULTIPLY BY 2 ELSE ADD 1 inside it, END, OUTPUT. A trace table with the pass, the line and the number: 3; pass 1, 3 < 10 yes, 3 to 6; pass 2, 6 < 10 yes, 6 to 12; pass 3, 12 < 10 no, 12 to 13; pass 4, 13 < 10 no, 13 to 14; OUTPUT 14. The last step asks about a fish-tank heater that starts at 22 degrees.
See the whole ideathe big idea and the key word, in one place
The big idea
Loops and IFs are control structures; an IF inside a loop asks its question again on every pass.
Pass by pass3 < 10, yes: 6. 6 < 10, yes: 12. 12 < 10, no: 13. 13 < 10, no: 14.
Easy to get wrongDo not answer the question once for the whole loop. Ask it again, with the new number, on every pass.
Key word
- control structure
- a part of code that decides which lines run, or how many times: a loop or an IF
Ready to use it?
- Trace a loop that has an IF inside it.