Loops
Repeat instructions safely with WHILE and FOR loops in MiniScript+.
On this page
Loops repeat a block of instructions. They are useful for processing a sequence, validating input, or running a calculation several times.
WHILE loops
A WHILE loop repeats while its condition remains true.
counter = 0WHILE counter < 3 PRINT counter counter = counter + 1ENDThe output is:
012The loop has three parts:
- initialize the state before the loop;
- check a condition before every iteration;
- update the state inside the loop.
FOR loops
Use FOR when the start, end, and step are known in advance.
FOR number FROM 1 TO 5 PRINT numberENDAdd INCR to change the increment:
FOR number FROM 10 TO 0 INCR -2 PRINT numberENDChoose the right loop
| Situation | Recommended loop |
|---|---|
| A known number of repetitions | FOR |
| Repeat until a condition changes | WHILE |
| Walk through a numeric interval | FOR |
| Validate repeated user input | WHILE |
Avoid infinite loops
An infinite loop never makes its condition false.
counter = 0WHILE counter < 5 PRINT counterENDThe update is missing
The example never changes
counter. Addcounter = counter + 1inside the loop before running it.
Nested loops
A loop may contain another loop. Use indentation to keep the structure readable.
FOR row FROM 1 TO 3 FOR column FROM 1 TO 3 PRINT row * column ENDENDNested loops can perform many operations, so check the expected number of iterations before using them with large ranges.
Trace a loop
For the first WHILE example, build a trace table before running the code:
| counter before | counter < 3 | Output | counter after |
|---|---|---|---|
| 0 | TRUE | 0 | 1 |
| 1 | TRUE | 1 | 2 |
| 2 | TRUE | 2 | 3 |
| 3 | FALSE | — | — |
The condition is checked again after the last iteration. If it starts false, the body runs zero times. Include an empty sequence in your reasoning.
FOR bounds and increments
Both bounds are inclusive. FOR i FROM 1 TO 5 runs five times, not four. A positive increment with a starting value above the ending value produces zero iterations. Counting down requires a negative increment. A zero increment is an error.
Start, end, and increment expressions are evaluated once on entering the loop. Changing a variable used in the bound expression does not update that stored bound. Avoid changing the control variable inside the body. After normal completion, it contains the first value outside the range: after 1 TO 5, it is 6.
The 10 TO 0 INCR -2 example prints 10, 8, 6, 4, 2, 0: six values. Use separate control names for nested loops. A three-step loop inside another three-step loop executes its inner body nine times.
Practice
- Print even numbers from
2through10: there are five values. - Sum
1through5, initializing the total before the loop: expect15. - Start the first
WHILEexample at3and explain why it prints nothing. - Fix the infinite example before running it. The editor's step limit detects long execution but cannot repair an algorithm.
Apply these patterns to sums and counts and string traversal.