For loop

For loops are the most complex loops in ChaosPro. They behave like their C counterparts. The syntax of a for loop is:

  for (expr1; expr2; expr3)  {    statement;  }  

Or a bit more explanative:

  for (init_expr; conditional_expr; iter_expr)  {    statement;  }    

The first expression (expr1 or init_expr) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 (or conditional_expr) is evaluated. This expression must be an expression returning a boolean value. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.

At the end of each iteration, expr3 (or iter_expr) is evaluated (executed).

Each of the expressions can be empty. expr2 being empty means the loop should be run indefinitely (ChaosPro implicitly considers it as TRUE, like C). This may not be as useless as you might think, since often you’d want to end the loop using a conditional break statement instead of using a boolean expression.

You may specify more than one expression for init_expr and for iter_expr by separating them using a comma. This way you can perform several initialization statements and several iteration statements

Consider the following examples. All of them sum up all numbers from 1 to 10:

  /* example 1 */  sum=0;  for (i = 1; i <= 10; i=i+1)  {    sum=sum+i;  }    /* example 2 */    sum=0;  for (i = 1;;i=i+1)  {    if (i > 10)    {      break;    }  }    /* example 3 */    i = 1;  for (;;)  {    if (i > 10)    {      break;    }    i=i+1;  }    /* example 4 */    for (i = 1,sum=0; i <= 10; sum=sum+i,i=i+1)  {    ;  }  

Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in for loops comes in handy in many occasions.

for