Practice question · Select all that apply
A loop runs: i = 10; while i > 0 do { print i; i = i - 3 }. Select every value that gets printed.
Hints
- Print happens before the update, and the test happens before the print.
- Follow i down: 10, 7, 4, 1, and then what?
Show the answer
- B. 7
- C. 1
- D. 4
- E. 10
Why
10, 7, 4 and 1 are printed; i then becomes -2, the test i > 0 fails and the loop ends before printing again. Because the counter steps by 3 it never lands on 0, a condition of 'i is not 0' would have looped forever.
Practise Loops and Iteration
The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.