Iteration Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Iteration

A

The process of repeating a set of instructions for a fixed number of times or until there is a desired outcome

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Count controlled iteration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Condition controlled iteration

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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]

A
number = input("Please enter a number.")
for times = 1 to 12
    print(number + "x" + times "=" +
    number*times)
next times
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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]

A

(There are many suitable algorithms that could be used, any working algorithm is acceptable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly