# 3.3.2.20 For

The for command is identical to the C For loop.

## Syntax:

for ( [expression1] ; expression2 ; [expression3] ) {script}

First, expression1 is evaluated. This specifies initialization for the loop. Second, expression2 is evaluated and if true, then script is executed. (If no conditional operator is used in expression2, any non-zero value is accepted as true.) Third, expression3, often an incrementation, is executed. The loop terminates when expression2 is found to be false. Expression1 and expression3 (which are optional) can consist of multiple statements, each separated by a comma.

 Note: If your script loops endlessly, you can interrupt the script by pressing ESC.

Note on While Loop Simulation

LabTalk does not have a While command. However, the While command can be easily simulated with the for loop. The While loop would look like this:

expression1;
while (expression2)
{
statement;
expression3;
}

The equivalent for loop syntax is:

for (expression1; expression2; expression3)
statement;

where either expression3 or statement would be responsible for setting the conditions that would cause expression2 to evaluate to false.

This for loop script simulates a While loop. This script continues to prompt for new values until you enter zero or a negative number.

p = 1;
for ( ;p > 0; )
getn -s (Enter 0 to quit) p (Next value);

None

## Examples:

The following for loop:

for (ii = 1, jj = 1 ; ii<4 && jj<4 ; ii++)
{
type $(ii)$(jj);
if (ii == 3)
{
ii = 0;
jj++;
}
}

Produces the output:

11
21
31
12
22
32
13
23
33