Skip to content
S3U
Login Create Account

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 first

Programs follow precise instructions

A program is a set of instructions. A variable stores a value. A condition chooses between paths. A loop repeats steps. Output is the result produced.

How to work it out Trace one instruction at a time. Keep a record of values and check exactly when a condition or repeated step is evaluated.

A separate example In a separate trace, start with n = 2, add 3, then double n. The two instructions happen in order: 5 is an intermediate value before the final result.

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?