Iteration Flashcards
Iteration
The process of repeating a set of instructions for a fixed number of times or until there is a desired outcome
Count controlled iteration
Used when the number of iterations is known before the loop is started
for loops: instruct the loop to be executed for a set number of times
for turns = 0 to 3
print(turns)
next turns
while loops: can also be used for count controlled iteration
turns = 0 while turns <= 3 print(turns) turns = turns + 1 endwhile
This is less efficient than the for loop
Condition controlled iteration
Used when the number of times a loop is executed is determined by a condition
while loops: continue while a condition remains true and stop when it becomes false
number = 0
while number != 3
number = input(“Please enter a number”)
endwhile
do… until loop: continues until a condition becomes true
do
number = input(“Please enter a number”)
until number == 3
An until loop checks the condition at the end, and a while loop checks it at the start, so only the until loop is guaranteed to run at least once
Wrote an algorithm in pseudocode that will print out a times table for any number entered by a user. The table should run from times 1 to 12. [4]
number = input("Please enter a number.") for times = 1 to 12 print(number + "x" + times "=" + number*times) next times
Write an algorithm in pseudocode for a game that will generate a random number between 1 and 10 for a user to guess. If they guess correctly they are old they are correct. If they do not guess correctly, they are told the randomly generated number. [6]
(There are many suitable algorithms that could be used, any working algorithm is acceptable)