• No results found

Driving LEDs

7.2 Iterative Loops

7.2.1 The for Loop

Thefor loop is an iterative loop. It executes one or more statements repeatedly. In general, a for statement takes the form shown in Figure 7-1. The braces in the for statement are essential only if the body has a compound statement. If the body is a single statement, the braces may be used but are not essential.

C++ Compound statement

A Compound statement or block is a number of single statements grouped together between matching braces ({}).

Figure 7-1 An example of a for loop.

Three expressions are enclosed within the pair of parentheses belonging to the for loop. The first of these statements is:

for(i = 0; i < 10000; i++) {

statements }

The body of the for loop (braces are necessary if the body has more than one statement).

Initialising expression Test expression Incremental Expression

i = 0;

This expression is executed only once at the start of the for statement and is known as the initialising expression. The initialising expression can be quite complex. It may be used to initialise a number of variables. In general, these variables are known as loop counters. In the preceding example, the value of i is

used to keep a count of the number of times the for loop is executed; hence the name loop counter. In C++, the initialising expression may even include variable declarations (e.g. int i = 0;). Note that if the initialising expression is omitted the semicolon must still be used.

The second expression:

i < 10000;

is known as the test expression. This expression is evaluated just before the body of thefor loop is executed. The result of evaluating this expression is considered in a logical sense. That is, it will be tested to determine whether the expression evaluates to true (one) or false (zero).

C++ trueorfalse

A program that is given any values that are zero are considered to be false; non- zero values are considered to be true.

When a program evaluates a logical expression, if the condition is true the result will be 1. If the condition is false the result will be zero.

In this particular case, the test expression tests whether the value of i is less than 10000. If the value of i is less than 10000 the expression evaluates to true, otherwise false. The body of the for statement will be executed immediately after the test expression, if and only if the test expression evaluates to true. If the test expression evaluates to false, the for statement terminates without executing the statements in its body.

The left angle bracket (<) is known as the less than operator. These operators belong to a class of operators named relational operators. Due to the presence of the relational operator, the expression (i< 10000) is known as a relational expression.

C++ Relational Operators

< less than > greater than

<= less than or equal to >= greater than or equal to

In addition to relational expressions, we can also use equality expressions. These expressions contain equality operators.

C++ Equality Operators

== equal to != not equal to

The third expression in the for statement is:

i++;

This statement is known as the incremental expression. It will be evaluated immediately after executing the body of the for statement. Usually, it increments one or more loop counters. In this particular case it increments the value of i by 1. The++ operator can be used in two different ways. Using it before the identifier will cause a pre-increment, e.g. ++i. Using it after the identifier will cause a post- increment, e.g. i++. In the case of ‘pre’ operations, the operation (operation meaning increment or decrement) is carried out before using the identifier in the test expression. In the case of ‘post’ operations, the operation is carried out after

using the identifier in the test expression. The -- operator is used exactly the same way; the only difference being that it will cause a decrement. These operators fall into a category known as unary operators. They are referred to as unary operators because they operate on just one argument.

C++ Unary Operators

+ unary plus

- unary minus

++ pre-increment (prefix) or post-increment (postfix) -- pre-decrement (prefix) or post-decrement (postfix) ~ bitwise complement. Toggles bit by bit.

! logical negation. Change true to false and vice-versa.

The code fragment shown below demonstrates the operation of the for loop. It also shows how a for loop operates inside another (nested for loops):

int i, j;

for (i = 0; i < 5; i++) {

for(j = 0; j < i; j++) cout << ‘*’;

cout << endl; }

Implementing this code in a program will produce the output shown in Figure 7-2: *

** *** **** *****

Figure 7-2 The output of the nested for loop operation.

The body of the outer for loop starts at the open brace and ends at the close brace. Between these two braces is the inner for loop. The inner for loop has no braces because it only has the one statement as its body:

cout << ‘*’;

The second statement of the outer for loop is:

cout << endl;

and will be executed after the inner for loop has completed all its iterations. Each iteration of the inner for loop prints the character ‘*’ on the screen and increments the loop counter j. Printing for that line ceases when the value of j reaches that of the outer loop counter i. Therefore, each iteration of the outer for loop consists of j iterations of the inner for loop.

7.2.2 The while Loop and the do–while Loop

Repetitive iterations as performed with the for loop can be carried out using the

while loop. The while loop is similar to the for loop without initialising and

incremental expressions. It simply has a test expression enclosed within a pair of parentheses that is evaluated at the start of the loop. This expression must evaluate to true for the body of the while loop to be executed. If it evaluates to false (zero) the loop will be terminated. It is possible that the body of the while loop is not executed at all - if in the first entry of the loop, the test expression evaluates to false or zero.

Because initialising and incremental expressions are omitted, while loops do not generally deploy a loop counter. However, the while loop can be used to implement the behaviour of a for loop and vice-versa. In general, while loops are implemented when the exact number of iterations are not known.

Figure 7-3 The while and do-while loops.

The do-while statement is very similar to the while statement. The difference being that the test expression is evaluated at the end of the loop resulting in at least one execution of the body of the loop. The statements in the body of the loop are between the keyword do and the keyword while. Braces must be used if the body is a compound statement. Figure 7-3 shows the anatomy of the two types of loop.