Practice While Loop in CS Thinking
Use these practice problems to test your method after reviewing the concept explanation and worked examples.
Quick Recap
A control structure that repeats a block of code as long as a specified condition remains true.
A while loop is like 'keep going until...' โ keep stirring the soup WHILE it's not boiling. Keep asking for a password WHILE the password is wrong.
Example 1
mediumTrace: SET n = 1. WHILE n < 100: n = n * 2. OUTPUT n. How many iterations?
Example 2
mediumWrite a WHILE loop that asks the user for input until they type 'quit'.
Example 3
mediumTrace: SET total = 0. SET i = 1. WHILE i <= 4: total = total + i. i = i + 1. OUTPUT total.
Example 4
mediumTrace this code and state the final output:
SET n = 1
SET result = 1
WHILE n < 5
SET n = n + 1
SET result = result * n
END WHILE
OUTPUT result