ExamplesLoops
Loops
Counters, totals, countdowns, and repeated input implemented with MiniScript+ loops.
On this page
Loops repeat the same operation while a condition holds or across a known interval.
Count upward
counter.msp
counter = 0WHILE counter < 5 PRINT counter counter = counter + 1ENDExpected result: 0, 1, 2, 3, 4.
Sum from 1 to N
sum.msp
n = 5total = 0FOR value FROM 1 TO n total = total + valueENDPRINT totalExpected result: 15.
Countdown
countdown.msp
counter = 5WHILE counter >= 0 PRINT counter counter = counter - 1ENDExpected result: 5, 4, 3, 2, 1, 0.
Process repeated input
three-values.msp
total = 0FOR index FROM 1 TO 3 INPUT value total = total + valueENDPRINT totalExpected result: 6. Submit 1, 2, 3 at the three prompts, one value at a time.
Every
WHILEloop must make progress toward a false condition. Otherwise it will run indefinitely.
Trace state
In the sum example, the total becomes 1, 3, 6, 10, 15. Initialize it before the loop; putting total = 0 inside the body would discard the accumulated sum. FOR includes both bounds. Set n to 0: zero iterations leave the sum at 0. Read loops and sums and counts.