Year 10 Computing — Repeating while something is true
Follow a loop that keeps going while a condition holds, working out what it leaves behind and how many times it went round.
Name: ________________________
1.This loop doubles a number until the condition stops holding. What does the program print?
n = 1 while n < 20: n = n * 2 print(n)2.This loop is left holding a value the condition would not have allowed it to start from. What does the program print?
n = 64 steps = 0 while n > 5: n = n - 12 steps = steps + 1 print(n)3.In each of these a variable starts at 1 and is multiplied by the number shown every time round, and the loop keeps going while the variable is below 100. Match each multiplier to the value the variable is left holding.
- Multiplied by 2 each time
- Multiplied by 3 each time
- Multiplied by 5 each time
- Multiplied by 10 each time
- Multiplied by 4 each time
- 243
- 128
- 125
- 100
- 256
4.A second variable here does nothing but count the trips. What does the program print?
count = 0 total = 0 while total < 30: total = total + 7 count = count + 1 print(count)5.A loop's condition is that n is greater than 0, its body adds 1 to n, and n holds 10 when the loop is reached. What happens when this program is run?
- a) It goes round once
- b) It does not go round at all
- c) It never stops on its own
- d) It goes round ten times
6.Look at the condition before you follow anything. What does this program print?
n = 5 while n > 10: n = n + 1 print(n)- a) 10
- b) 11
- c) 5
- d) 6
7.Money is put by each week until there is enough. What does this program print?
money = 50 weeks = 0 while money < 200: money = money + 25 weeks = weeks + 1 print(weeks)8.Both variables change every time round here, and only one of them is in the condition. What does the program print?
total = 0 n = 1 while total < 20: total = total + n n = n + 1 print(n)
Answer key — Year 10 Computing — Repeating while something is true
- 1. 32
- 2. 4
- 3. Multiplied by 2 each time → 128; Multiplied by 3 each time → 243; Multiplied by 5 each time → 125; Multiplied by 10 each time → 100; Multiplied by 4 each time → 256
- 4. 5
- 5. c) It never stops on its own
- 6. c) 5
- 7. 6
- 8. 7