Definite & Indefinite Iteration Flashcards
All 8 cards in this deck
What is the difference between definite and indefinite iteration?
Definite (count-controlled) iteration repeats a set number of times, e.g. a
FORloop. Indefinite (condition-controlled) iteration repeats until or while a condition is met, e.g.WHILEorREPEAT … UNTIL.What is the difference between a
WHILEloop and aREPEAT … UNTILloop?WHILEtests its condition at the start, so the code inside may run zero times.REPEAT … UNTILtests its condition at the end, so the code inside always runs at least once.How is a count-controlled loop written in AQA pseudo-code?
e.g. output the numbers 1 to 5
- Set the counter, start and end:
FOR i ← 1 TO 5 - Put the repeated code inside:
OUTPUT i - Close the loop:
ENDFOR
- Set the counter, start and end:
True or false? A
FORloop can only count up in steps of 1.False. A step can be given, e.g.
FOR i ← 0 TO 10 STEP 2, and a negative step counts down.How is a
WHILEloop written in AQA pseudo-code?e.g. keep doubling
total, which starts at 1, while it is under 50- Set the variable used in the condition before the loop:
total ← 1 - Write
WHILEand the condition:WHILE total < 50 - Write the code to repeat, then close the loop:
total ← total * 2thenENDWHILE
- Set the variable used in the condition before the loop:
When does a
WHILEloop, and when does aREPEAT … UNTILloop, become an infinite loop?A
WHILEloop never ends if its condition is always true. AREPEAT … UNTILloop never ends if its condition is never true.How does a
DO … WHILEloop work?e.g.
DO … WHILE NotSolvedIts condition is tested at the end, so the code inside always runs at least once. It repeats while the condition is true, here while
NotSolvedis true.How do you choose between definite (
FOR) and indefinite (WHILEorREPEAT … UNTIL) iteration?Use definite iteration when the number of repetitions is known before the loop starts, e.g. outputting each of the 10 items in an array. Use indefinite iteration when it depends on a condition, e.g. asking for a password again until the correct one is entered.