2005/5/10

     
 

CfgScript Loop Statements

artefaktur

| Expression statement | block {} Statement | Branch Statements | Loop Statements | Type Declaration | Variable Declaration | Synchronize Statements | With Statement | Using Statement | Type Alias |

Various loops, like do/while, while, for, foreach are available


Content of this chapter:

   LoopStatement
       WhileStatement
       DoWhileStatement
       ForStatement
       ForEachStatement



 LoopStatement

LoopStatement
:  WhileStatement
|  DoWhileStatement
|  ForStatement
|  ForEachStatement
;

 WhileStatement

WhileStatement
: 'while' '('  Expression ')' 
               Statement
;
Sample:

int i = 1;
while (i > 1 && i < 5)
{
  if (i == 2)
  {
    i = 3;
    continue;
  }
  if (i == 4)
    break;
  i = i + 1;
}

 DoWhileStatement

DoWhileStatement
: 'do'  Statement 
  'while' '('  Expression ')' ';'
;

Sample:

StringBuffer sb = new StringBuffer("");
do {
  sb.append(" ");
} while (sb.length() < 10);

 ForStatement

ForStatement
: 'for' '(' (VarDecl |  Expression) ';' 
                        Expression ';' 
                        Expression ')' 
         Statement
;

for (int i = 0; i < 5; ++i)
{
  if (i < 2)
    continue;
  if (i > 3)
    break;
}


 ForEachStatement

ForStatement
: 'foreach' '(' VarDecl 'in'  Expression ')' 
       Statement

The Expression has to be one of following types:
In all other cases the the loop will initialized with the Expression value and will looping once with this value.

Sample:

ObjectArray array = [ 1, 2, 3, 4, 5, 6, 7 ];

bool reachedEnd = false;
foreach (i in array)
{
 
  if (i == 2)
    continue;
  out.println("foreach: " + i);
  if (i == 4)
  {
    reachedEnd = true;
    break;
  }
  if (i == 5)
    reachedEnd = false;
}