ScripticXLearn programming visually
Platform
Problems
Community
Leaderboard
Learn
Docs
MiniScript+ basicsVariablesOperators and expressionsInput and outputConditions and decisionsLoopsStringsSums, counts, and extremaDigits of a numberDivisibility and prime numbersNumeric sequences and recurrencesNumber basesFunctions and parametersRecursion
Examples
Basic programsConditionsInput and outputLoopsAlgorithmsWorking with textDigit problemsBase conversionsFunctions and recursionEncode text with a key
What's newHelpContact
Loading…

Documentation

MiniScript+

DocumentationMiniScript+
Getting started
MiniScript+ basics
Language basics
VariablesOperators and expressionsInput and outputConditions and decisionsLoopsStrings
Elementary algorithms
Sums, counts, and extremaDigits of a numberDivisibility and prime numbersNumeric sequences and recurrencesNumber bases
Functions
Functions and parametersRecursion
Documentation/Loops

Loops

Repeat instructions safely with WHILE and FOR loops in MiniScript+.

On this page
WHILE loopsFOR loopsChoose the right loopAvoid infinite loopsNested loopsTrace a loopFOR bounds and incrementsPractice

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.

MS+MiniScript+
1counter = 02​3WHILE counter < 34  PRINT counter5  counter = counter + 16END

The output is:

TXTPlain text
102132

The loop has three parts:

  1. initialize the state before the loop;
  2. check a condition before every iteration;
  3. update the state inside the loop.

FOR loops#

Use FOR when the start, end, and step are known in advance.

MS+MiniScript+
1FOR number FROM 1 TO 52  PRINT number3END

Add INCR to change the increment:

MS+MiniScript+
1FOR number FROM 10 TO 0 INCR -22  PRINT number3END

Choose the right loop#

SituationRecommended loop
A known number of repetitionsFOR
Repeat until a condition changesWHILE
Walk through a numeric intervalFOR
Validate repeated user inputWHILE

Avoid infinite loops#

An infinite loop never makes its condition false.

MS+MiniScript+
1counter = 02WHILE counter < 53  PRINT counter4END

The update is missing

The example never changes counter. Add counter = counter + 1 inside the loop before running it.

Nested loops#

A loop may contain another loop. Use indentation to keep the structure readable.

MS+MiniScript+
1FOR row FROM 1 TO 32  FOR column FROM 1 TO 33    PRINT row * column4  END5END

Nested 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 beforecounter < 3Outputcounter after
0TRUE01
1TRUE12
2TRUE23
3FALSE——

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#

  1. Print even numbers from 2 through 10: there are five values.
  2. Sum 1 through 5, initializing the total before the loop: expect 15.
  3. Start the first WHILE example at 3 and explain why it prints nothing.
  4. 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.

PreviousConditions and decisionsNextStrings

On this page

WHILE loopsFOR loopsChoose the right loopAvoid infinite loopsNested loopsTrace a loopFOR bounds and incrementsPractice