The FOR Statement
The for statement has the following form:
for ( init-expression; cond-expression; loop-expression ) statement
Execution of a for statement proceeds as follows:
1. The init-expression, if any, is evaluated. This is normally used to establish the initial values for the variables used to control the loop.
2. The cond-expression, if any, is evaluated. If the expression evaluates to true (any non-zero value) the statement body is executed.
3. After each iteration of the statement body, the loop-expression is evaluated, then the cond-expression is evaluated. If the cond-expression is still true, the statement body is repeated. This process continues until the cond-expression evaluates to be false (0).
The most typical usage of a for loop is to repeat a statement body a fixed number of times as in the following example:
/* In this example, the program calculates the Nth power (in this case N = 5) of a predefined rXvalue. */ ÃÂ rXvalue = 2.5; iNth = 5; for (iPower=0; iPower < iNth; iPower++) rXvalue *= rXvalue;
The IF Statement
The if statement tests a condition and if true (non-zero) executes the if statement body, or if false, and an else clause exists, executes the else statement body. Following are examples:
/* This example also illustrates the difference between the comparison operator "==" and the assignment operator "=". The first operator compares both sides of the equation to see if they are equal, while the second assigns the value on the right side to the variable on the left side. */ ÃÂ if (rData == rDUMMY) rX = rDUMMY; if (rData == 0.0) { rY = 0.0; rX = rDUMMY; } else { rY = rDUMMY; rX = 0.0; }
The SWITCHStatement
The switch statement helps control complex conditional and branching operations. The switch statement defines a branching structure for testing integer values and performing certain actions depending on the integer value that is retrieved. An example is:
/* Depending on the value of iTest, rXvalue or rYvalue or neither is increased. */ switch (iTest) { case 0: rXvalue = rXvalue + .25 ; break: case 1: rYvalue = rYvalue + .25; break; case 2:ÃÂ // Do not increment x or y when iTest = 2 break; default: ÃÂ // Control will come here if no cases match the iTestÃÂ // do something else }
The switch causes program control to move to the case line that has the same value as iTest. Note that once control has advanced to the appropriate case, each statement will be processed in order, including
advancing into the next case or default section. The break statement is commonly used to limit execution to the statements within one case.
The WHILE Statement
The while statement lets you repeat a statement or compound statement until a specified expression becomes false (zero). For example:
/* This code will iterate over every element in the rData arrayÃÂ until it reaches a dummy value.ÃÂ ÃÂ If rData[0] is DUMMY then the value of rY is undefined. */ real(5) rData; int i, iLength; real rY; ÃÂ iLength = 5; i = 0; while ( (i < iLength) && (rData[i] != rDummy) ) { rY = rData[i]; i++; }
The DO-WHILE Statement
The do-while statement is a variant of the while statement. Like while, it executes until a condition is false, however it moves the evaluation to the end of the compound statement which means that the statement or compound statement is always executed at least once. For example:
// in this example if rData[0] is DUMMY then rY will equal rDUMMY i = 0; do { rY = rData[i]; i++; } while ( (i < iLength) && (rY != rDummy) );
The BREAK Statement
The break statement terminates the execution of the nearest enclosing do, for, switch or while statement in which it appears. In a switch statement, break is commonly used as the last statement in each case block to prevent program flow moving into the next case block. In a do, for, or while loop, the break statement can be used to terminate the loop prematurely. See the switch code example above for an example.
The CONTINUE Statement
The continue statement passes control to the next iteration of the nearest enclosing do, for or while statement, bypassing any remaining statements in the do, for or while statement body.
Within a do, or while statement, the next iteration starts by re-evaluating the expression of the do or while statement.
A continue statement in a for statement causes the last expression of the for statement to be evaluated, then the conditional statement is evaluated. Depending on the result, the statement body is iterated or the loop terminates.
Following is an example of a continue statement:
/* If rData[i] is equivalent to DUMMY nothing happens, otherwise rData[i] is set to 0.0. */ ÃÂ for (i=0;i<1000;i++) { if (rData[i] == rDUMMY) continue; rData[i] = 0.0; }
Expression statements
An expression statement is a single statement that is evaluated according to the operator rules defined in the Operators section of this manual. Expression statements always end with a ‘;’. Below are examples of expression statements:
rX = 4.5; // simple assignment rD = rSqrt(rX*rX + rY*rY); // function call returns a real value rX = rY = 0.0; // 0.0 is assigned to both rX and rY
Compound statements
A compound statement (also called a “block”) typically appears as the body of another statement, such as the if statement. Compound statements are a sequence of statements enclosed by curly braces ({ }). An example is:
if (Tb != NULLTB) { ProgName_SYS("Load levels",1); LockSymb_DB(Data,Level,DB_LOCK_READWRITE,DB_WAIT_INFINITY); TableLineFid_DU(Data,Level,Ref,LTBL,Tb); UnLockSymb_DB(Data,Level); }
Note: Unlike C, you cannot declare new variables within a compound statement.