Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

low control refers to the programming statements that control what happens next, how many times and under what conditions. The first group (forifswitchwhile and do-while) are looping statements, which refers to the fact that an expression statement or compound statement is executed repeatedly (ie in a loop) based on some condition, for example  "keep doing this until that happens" or "do this 10 times". The break and continue statements are used to alter the flow of the loop.

 

Statement

Description

for

loop a specified number of times

if

controls conditional branching

switch

transfer control based on a complex condition

while

while a condition is true, process a statement

do-while

repeat  while a condition is true

 break

breaks control out of a do, while, for or switch statement

continue

go immediately to the next iteration of a loop

 expression statementa single expression

compound statement

sequence of statements enclosed in braces

Table of Contents


 

Anchor
FOR
FOR
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:

Code Block
languagecpp
/* 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;

Anchor
IF
IF
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:

 

Code Block
languagecpp
/* 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;
}
When nesting if statements and else clauses, the compiler will associate an else statement with the most recent if statement that lacks an else. However, this can easily lead to errors in your code and we recommend that you use braces to clearly identify the associations of if and else statements.

Anchor
SWITCH
SWITCH
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:

 

Code Block
languagecpp
/* 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

Anchor
WHILE
WHILE
The WHILE Statement

The while statement lets you repeat a statement or compound statement until a specified expression becomes false (zero). For example:

 

Code Block
languagecpp
/* 	This code will iterate over every element in the rData array 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++;    
}

 

Anchor
DOWHILE
DOWHILE
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:

 

Code Block
languagecpp
// 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) );

 

Anchor
BREAK
BREAK
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.

 

 

Anchor
CONTINUE
CONTINUE
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:

 

Code Block
languagecpp
/*	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;
}

Anchor
EXPRESSION
EXPRESSION
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:

 

Code Block
languagecpp
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

 

Anchor
COMPOUND
COMPOUND
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:

 

Code Block
languagecpp
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.