4.2.4. for

Executes one or more statements a given number of times.

Syntax:

for 
    (expression_1;            /* evaluate only once */
     expression_2;            /* evaluate before each iteration */
     expression_3)            /* evaluate after each iteration */
{
    statement;                /* execute this statement */
                              /* while expression_2 is True */
    [statement;]...           /* additional statements */
}

Description

The for statement is useful for executing a statement a given number of times. It evaluates expression_1 and then evaluates expression_2 to see if it is True, that is nonzero, or False, that is zero. If expression_2 evaluates to True, all statements are executed once.

Next expression_3 is evaluated, and expression_2 is evaluated again to see if it is True or False. If expression_2 is True, all statements are executed again and the cycle continues. If expression_2 is False, all statements are bypassed and execution continues at the next statement outside the for loop.

Where you have more than one statement in the for loop these must be enclosed by curly braces ({}).

The term expression_1 can be used to initialize a variable to be used in the loop. It is evaluated once, before the first iteration of the loop. The term expression_2 determines whether to execute or terminate the loop and is evaluated before each iteration. If the term expression_2 evaluates to True, that is nonzero, the loop is executed. If expression_2 is False, that is zero, the loop is terminated. The term expression_3 can be used to increment a loop counter, and is evaluated after each iteration.

Return Value

None

Rules

None

Example

This example shows how to use the for statement in a macro:

define /R void forloop()
{
  int i;
  for (i=0; i<11; i++) {
    if (i > 10) {
      $printf "    Done!\n"$;
      break;
    } else if (i==5) {
      $printf "    Halfway there...\n"$;
      continue;
    }
    $printf "Iteration: %d\n", i$;
  }
}
.

See also

Copyright © 2002-2011 ARM. All rights reserved.ARM DUI 0175N
Non-ConfidentialID052111