Table of Contents

How To Repeat Code With Loops

Use loops when a script needs to do the same kind of work more than once.

Examples:

Expr has three main loop forms:


Repeat A Fixed Number Of Times

Use `loop(count)` when you already know how many times to repeat the code.

count = 0;
 
loop(5)
{
    count += 1;
};
 
count;

The final result is `5`.

The loop count is evaluated once before the loop starts.

repeats = 3;
count = 0;
 
loop(repeats)
{
    count += 1;
};
 
count;

The final result is `3`.

Use `loop` for simple repeat actions.

loop(3)
{
    print('probe sample');
};

Add Values In A Loop

Loops are often used to build a result step by step.

sum = 0;
 
loop(4)
{
    sum += 10;
};
 
sum;

The final result is `40`.

For real scripts, this could be sample count, retry count, accumulated distance, or number of completed steps.


Use while For A Condition

Use `while` when the loop should continue while a condition is true.

value = 1;
 
while(value < 10)
{
    value *= 2;
};
 
value;

The final result is `16`.

The condition is checked before each iteration.

A `while` loop should also have a clear way to stop. This example stops when `ready` becomes true, or after three checks:

ready = false;
checks = 0;
 
while(!ready && checks < 3)
{
    checks += 1;
 
    if(checks == 2)
    {
        ready = true;
    };
};
 
ready;

The final result is `true`.


Avoid Infinite Loops

A `while` loop must change something that can make the condition false.

This is good:

count = 0;
 
while(count < 5)
{
    count += 1;
};

This is wrong because `count` never changes:

count = 0;
 
while(count < 5)
{
    print(count);
};

Expr has safety limits, but scripts should still be written so loops finish normally.


Use for For Counters

Use `for` when a loop has a counter.

sum = 0;
 
for(i = 0; i < 5; i += 1)
{
    sum += i;
};
 
sum;

The final result is `10` because the loop adds:

0 + 1 + 2 + 3 + 4

A `for` loop has three parts:

for(initial_value; condition; step)
{
    // repeated code
};

The order is:

  1. initialize the counter
  2. check the condition
  3. run the loop body
  4. run the step expression
  5. check the condition again

Count From 1 Instead Of 0

Use whatever counter range makes the script easiest to read.

text = '';
 
for(pass = 1; pass <= 3; pass += 1)
{
    print('pass ', pass);
};

This prints pass numbers `1`, `2`, and `3`.

For arrays and indexed data, zero-based counters are often useful. For machine passes or retries, one-based counters may be easier to read.


Stop Early With break

Use `break` to exit the nearest loop.

count = 0;
 
while(true)
{
    count += 1;
 
    if(count >= 3)
    {
        break;
    };
};
 
count;

The final result is `3`.

`break` is useful when the loop should stop as soon as a condition is reached.

found = false;
 
for(tool = 1; tool <= 10; tool += 1)
{
    if(tool == 4)
    {
        found = true;
        break;
    };
};
 
found;

The final result is `true`.


Skip One Iteration With continue

Use `continue` to skip the rest of the current loop body and move to the next iteration.

sum = 0;
 
for(i = 0; i < 5; i += 1)
{
    if(i == 2)
    {
        continue;
    };
 
    sum += i;
};
 
sum;

The final result is `8` because the loop adds:

0 + 1 + 3 + 4

The value `2` is skipped.

In a `for` loop, the step expression still runs after `continue`.


Use Loops From MDI

In MDI, start with `=` to evaluate Expr.

=sum=0;
for(i=1; i<=4; i+=1)
{
    sum += i;
};
sum;

This outputs:

10

Common Mistakes

Forgetting To Change The while Condition

This loop does not change `count`, so it cannot finish normally:

count = 0;
 
while(count < 5)
{
    print(count);
};

Fix it by updating `count` inside the loop:

count = 0;
 
while(count < 5)
{
    print(count);
    count += 1;
};

Using loop When You Need A Counter

`loop(5)` repeats five times, but it does not create a counter variable for you.

Use `for` when the current number matters:

for(i = 0; i < 5; i += 1)
{
    print(i);
};

Forgetting The Semicolon After The Loop

When a loop is used as a statement, end it with `;` after the closing brace.

loop(3)
{
    print('step');
};

Putting Too Much Work In One Loop

Long loops are harder to check. If a loop does several unrelated things, consider moving part of the work into a function.


Which Loop Should I Use?

Situation Good choice
Repeat exactly 3 times loop(3)
Repeat while a value is below a limit while(value < limit)
Count from 0 to 9 for(i = 0; i < 10; i += 1)
Stop as soon as a condition is found break inside a loop
Skip one value but continue looping continue inside a loop

Try This

In MDI, try:

=total=0;
for(pass=1; pass<=3; pass+=1)
{
    total += pass * 10;
};
total;

The final result is `60`.


See Also


Previous: Use if and else

Next: Use functions

How-To index: How-To Guides