StudyVault
Home Unit Overview Exam Technique Revision Techniques ← Previous Lesson Next Lesson →

← Back to Dashboard

Overlay:
0:00 / 0:00 Narration generated using AI text-to-speech.

Iteration, Selection and Operators

Iteration: Repeating Instructions

Iteration allows a program to repeat a block of code without the programmer writing it out multiple times. There are two categories: definite iteration, where the number of repetitions is known in advance, and indefinite iteration, where the loop continues until a condition becomes true or false. You need a theoretical understanding of all four loop types, even if your school’s chosen programming language does not support all of them.

Definite Iteration — The FOR Loop

Definite iteration uses a FOR loop. The loop variable counts from a start value to an end value, incrementing by one on each pass. In AQA pseudo-code:

FOR i ← 1 TO 5
  OUTPUT i
ENDFOR

This outputs 1, 2, 3, 4, 5 — five iterations, one for each value of i.

Use a FOR loop when you know exactly how many times the loop should run — for example, processing each of ten exam answers, or generating a times table from 1 to 12.

Indefinite Iteration — Condition at the Start: WHILE

Indefinite iteration continues until a condition changes. The WHILE loop checks its condition before each iteration. If the condition is false when the loop is first reached, the body never executes at all.

WHILE password ≠ “secret”
  password ← USERINPUT
ENDWHILE
OUTPUT “Access granted”

This keeps asking for input while the password is wrong. If the user gets it right on the first attempt, the loop body runs once; if they never get it right, it runs indefinitely.

Indefinite Iteration — Condition at the End: REPEAT...UNTIL and DO...WHILE

Two loop forms check their condition after the body has run, which guarantees the body executes at least once.

REPEAT…UNTIL continues the body until the condition becomes true:

REPEAT
  pin ← USERINPUT
UNTIL pin = 1234
OUTPUT “PIN accepted”

The user is always asked for a PIN at least once, then again until they enter the correct value.

DO…WHILE continues the body while the condition remains true:

DO
  answer ← USERINPUT
WHILE answer ≠ “quit”

This is logically identical to REPEAT…UNTIL except for whether the condition is expressed as “stop when X” or “continue while X”.

Key Fact

Condition checked before body: WHILE (may never execute). Condition checked after body: REPEAT…UNTIL and DO…WHILE (always execute at least once). Count-controlled: FOR (fixed number of iterations). All four are part of the specification even if Python (which lacks DO…WHILE and REPEAT…UNTIL natively) is your school’s chosen language.

Scenario 1 — Print a 5-question quiz score card. You know exactly how many scores to print (5). Use a FOR loop.

Scenario 2 — Keep asking for a valid age until a number between 0 and 120 is entered. You don’t know how many attempts the user will need. The user must enter at least once. Use REPEAT…UNTIL or DO…WHILE.

Scenario 3 — Keep reading GPS data while the car is moving. The car might already be stopped when the program starts, so the reading loop might never need to run. Use WHILE (so the body can be skipped if the condition is immediately false).

Nested Iteration and Nested Selection

Loops and conditionals can be placed inside other loops and conditionals. This is called nesting. Nested structures are used for grid operations, table printing, and multi-condition checks.

A common exam mistake is incorrectly handling the boundary condition in a nested IF. For example:

IF score >= 40 THEN
  OUTPUT “Pass”
  IF score >= 70 THEN
    OUTPUT “Merit”
  ENDIF
ENDIF

Students often accidentally output “Merit” without checking that the outer IF’s condition was also met first. Always trace through nested conditions carefully.

Arithmetic operators: + (add), − (subtract), * (multiply), / (real division — result can be decimal), DIV (integer division — whole quotient only), MOD (remainder after integer division).

Relational operators (for comparisons in conditions): = (equal to), ≠ (not equal to), < (less than), > (greater than), ≤ (less than or equal to), ≥ (greater than or equal to). These return a Boolean result (true or false).

Boolean operators combine conditions: AND (both conditions must be true), OR (at least one must be true), NOT (inverts a condition). Example: IF age ≥ 16 AND hasConsent = TRUE THEN — both conditions must hold.

Key Fact

Boolean operators are used to combine multiple conditions in a single IF or WHILE statement. AND requires all combined conditions to be true. OR requires at least one to be true. NOT inverts a Boolean value: NOT gameOver is true when gameOver is false.

Putting It Together — A Real Example

Consider a simple number-guessing game. The target number is 42. The player keeps guessing until they get it right or run out of attempts. This combines a WHILE loop (indefinite, condition at start), selection (IF to check the guess), Boolean operators, and counter variables:

target ← 42
attempts ← 0
maxAttempts ← 5
guess ← 0
WHILE guess ≠ target AND attempts < maxAttempts
  guess ← USERINPUT
  attempts ← attempts + 1
ENDWHILE
IF guess = target THEN
  OUTPUT “Correct!”
ELSE
  OUTPUT “Out of attempts”
ENDIF

Practice Questions

Test yourself on this lesson. Pick a question, write your answer, then get it marked.

AI feedback
AI feedback is guidance, not a final exam mark. If you disagree, your teacher's view is always the tie-breaker.
← Back to unit
Video

© 2026 StudyVault · About · FAQ · Copyright & IP · Privacy · Disclaimers

StudyVault Education Ltd · registered in England and Wales, company no. 17490559 · 71–75 Shelton Street, London WC2H 9JQ