Selection lets a program choose between paths using conditions. IF ... THEN ... ELSE ... ENDIF; ELSE IF for multiple options; nested IF statements put one decision inside another.
Example: IF mark ≥ 70 THEN OUTPUT "Distinction" ELSE IF mark ≥ 40 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF.
Selection chooses a path
Iteration
Count-controlled loops (FOR) repeat a set number of times: FOR i = 1 TO 5 ... NEXT i.
Condition-controlled loops (WHILE, REPEAT UNTIL) repeat until a condition is met. WHILE checks before; REPEAT UNTIL checks after (always runs at least once).
Nested loops: a loop inside a loop; useful for tables and 2D arrays.
Repeating instructions in a loop
Choosing
Use FOR when you know how many repeats; WHILE/REPEAT when you don't (e.g. keep asking for a password until correct).
Avoiding infinite loops
For WHILE, update the values used by the condition so it can become false. For REPEAT UNTIL, update them so the stopping condition can become true. Trace normal and boundary cases.
Trace a changing loop variable
Boolean conditions
Combine with AND, OR, NOT; use brackets for clarity.
Tracing
Use trace tables to follow loop values.
投影片
Sign up free to view the lesson slides
Step through every slide for this topic — plus flashcards and revision notes — with a free account.