Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.4j

Application v 5.22b

  

 

Chapter FourFlow Control (continued)

4.6 The ‘for’ Loop

4.6.1 Overview

The ‘for’ loop is defined by the for keyword. A boolean expression is contained within the ‘for’ loop syntax, and the code is executed repeatedly until that condition becomes false. ‘for’ loops feature explicit counting, where the counting variable is assigned and incremented in the loop statement itself. This is appropriate in situations where the number of iterations is clearly defined, such as moving through an array. 

4.6.2 Syntax and Structure

The for statement uses the following formal syntax:

for (init-expression; cond-expression; loop-expression) {

statement(s)
    }

A for loop consists of three parts: initialization, condition, and loop-expression. The following example illustrates a ‘for’ loop:

int   i;

for (i = 0; i < 10; i++) {

  MessageBox("This is the number %d", i);

  }

i = 0 represents the init-expression, i < 10 represents the cond-expression, and i++ is the loop-expression. The start point of the loop and its end point are clearly defined in the loop statement itself, as well as the manner in which the counting variable is incremented. The condition is tested at the top of loop; it should be noted that the loop might never be entered if the boolean condition does not result in TRUE at least once.

The init-expression and loop-expression may contain multiple items separated by commas. For example:

int   i, j;

for (i = 0, j = 10; i < 10; i++, j--) {

  MessageBox("This is the number %d and %d", i, j);

  }

The expressions can contain essentially anything. The cond-expression must resolve into a boolean.

It is the programmer’s responsibility to create looping structures and conditional expressions that result in a finite loop. Infinite loops may cause the application to behave unexpectedly or even hang so they must be used with care. The break and continue keywords can be used to control loop execution. In addition, the return or exit statements can end loop iteration.