S3U

Science / Programming basics / Grade 6 / sc670

Programming Basics: Make a While Loop Stop

Check a condition before each repetition and track progress toward stopping.

All worksheets
5 questions0m 0s
Learn the skill

WHILE checks its condition before each iteration. Trace SET count TO 0; WHILE count < 3: SET count TO count + 1; END; SHOW count. Use integer values and update your trace after each body execution.

Worked example

The condition is true at count 0, 1 and 2. Each pass adds 1. At count 3 the condition is false, so the loop stops and SHOW displays 3.

Starting at count zero, the condition count less than three is true for entry values zero, one and two. Each iteration adds one. At count three the condition is false and the loop exits.
Starting at count zero, the condition count less than three is true for entry values zero, one and two. Each iteration adds one. At count three the condition is false and the loop exits.
Question 1 What is count after the first body execution?
Question 2 How many times does the example execute its body?
Question 3 What does the final SHOW display?
Question 4 If count starts at 3 instead, how many body executions occur?
Question 5 If the update is removed while count starts at 0, what happens in this model?