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 FOR loop. Indefinite (condition-controlled) iteration repeats until or while a condition is met, e.g. WHILE or REPEAT … UNTIL.

What is the difference between a WHILE loop and a REPEAT … UNTIL loop?

WHILE tests its condition at the start, so the code inside may run zero times. REPEAT … UNTIL tests 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

  1. Set the counter, start and end: FOR i ← 1 TO 5
  2. Put the repeated code inside: OUTPUT i
  3. Close the loop: ENDFOR

True or false? A FOR loop 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 WHILE loop written in AQA pseudo-code?

e.g. keep doubling total, which starts at 1, while it is under 50

  1. Set the variable used in the condition before the loop: total ← 1
  2. Write WHILE and the condition: WHILE total < 50
  3. Write the code to repeat, then close the loop: total ← total * 2 then ENDWHILE

When does a WHILE loop, and when does a REPEAT … UNTIL loop, become an infinite loop?

A WHILE loop never ends if its condition is always true. A REPEAT … UNTIL loop never ends if its condition is never true.

How does a DO … WHILE loop work?

e.g. DO … WHILE NotSolved

Its condition is tested at the end, so the code inside always runs at least once. It repeats while the condition is true, here while NotSolved is true.

How do you choose between definite (FOR) and indefinite (WHILE or REPEAT … 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.