Program Flow and Control Structures

<< Click to Display Table of Contents >>

Navigation:  Concordance Programming Language Reference >

Program Flow and Control Structures

Concordance statements provide for the control and execution of program logic. Several types of conditional execution and repetitive execution statements are available.

All simple CPL statements are terminated by a semi-colon. Individual statements can be continued over several lines before being terminated with a the semi-colon.

Begin and End

Syntax:

{

statement

}

The { and } braces are used to create compound statements. Compound statements are treated as a single statement. { and } braces can be used after an if-else, while, or for to create a single block of statements that are executed if the test conditions are satisfied. Compound statements are terminated by the }, curly end-brace, a semi-colon does not follow the }. Individual statements within the compound statement must be terminated by semicolons.

 

Break

The break statement causes a cycle, while, or for loop to terminate before the loop control statement ends the loop. break causes the innermost loop to exit if one loop is enclosed by another loop. It also causes a case statement to exit the switch.

Syntax:

break;

Example:

cycle(db)

if (db->NAME == "Claire Ellen")

break;

 

Cycle Loops

Syntax:

cycle(database-handle)

statement

Cycle loops execute the controlled statement once for each document in the current query set. After completion, the last document in the retrieved set will be the current document.

Example:

bonus(int db)

{

cycle(db)

if (db->COMMISSION >= 100000)

db->BONUS = db->COMMISSION * 0.25;

else

db->BONUS = db->COMMISSION * 0.10;

}

 

For Loops

Syntax:

for(initialize; test-condition; increment expression)

statement

For loops contain three expressions: an initialization value, a test condition, and a loop increment expression. All three statements must be present. The initial loop value is set once by the initialization statement, the test condition is then evaluated. If the test condition evaluates to true, the controlled statement is executed. When the statement finishes, the increment expression is executed, the test condition is then re-evaluated and the loop is re-executed if it is true.

Example:

for(i = 0; i < 100; i = i + 1)

string[i] = 0;

This example would advance through the first 100 elements (from 0 to 99) of the array called string setting each element to zero.

for(i = goto(db,1); i > 0; i = next(db))

if (db->COMMISSION >= 100000)

db->BONUS = db->COMMISSION * 0.25;

else

db->BONUS = db->COMMISSION * 0.10;

The for loop used in this example produces the same results as the example used in the cycle statement on the previous page.

 

If-Else Statement

Syntax:

if(test-condition)

statement

else

statement

The if-else statement constitutes the most frequently used decision making syntax available in CPL. The statement evaluates the test condition and executes the first statement if it is true. If the test condition is false, the else statement is executed. else statements are optional.

Example:

if (i < j)

i = j;

else {

i = i - 1;

j = j + 1;

}

In this example, if i is less than j, i will be set equal to j. If i is not less than j the else clause is executed which subtracts one from i and add one to j.

 

Switch Statement

Syntax:

switch(expression)

{

case expression: statement;

break;

case expression: statement;

break;

case expression: statement;

break;

/* ... */

default:

statement;

break;

}

Switch statements are similar to multiple nested if-else statements. A switch statement evaluates its test expression and begin executing the statements following the case statement that matches the test expression. If no case statement matches, then program execution begins at the default statement. Execution continues until the next case, break, default, or } statement is encountered. default statements are optional. If no match is made and a default is not present, no action is taken. The case statements must be enclosed within a { } pair of braces.

Example:

for(i = 0; string[i]  0; i = i + 1)

switch(string[i])

{

case 'a':

case 'e':

case 'i':

case 'o':

case 'u': vowels = vowels + 1;

break;

case '.': periods = periods + 1;

break;

default: if (isalpha(string[i])

consonants = consonants + 1;

else

other = other + 1;

break;

}

 

While Loops

Syntax:

while(test-condition)

statement

A while loop evaluates the test condition and executes the controlled statement zero or more times. The while loop executes the statement if the condition is true. Execution continues until the test condition becomes false.

Example:

intClear(int numbers[])

{

int i, elements;

i = 0;

elements = sizeof(numbers)/sizeof(numbers[0]);

while(i < elements)

{

numbers[i] = 0;

i = i + 1;

}

}

This example would set every element of the numbers array to zero. One noteworthy feature is its use of sizeof(). intClear() does not know the number of elements in the array when it starts. It determines the number by dividing the size of the whole array by the size of one element. This function could be called in several places within a program to clear a variety of int arrays.