3.5 Do-While Loops
The do-while loop has the general form
do
statement;
while (expression);
Its behaviour is virtually the same as the while loop except that it always executes the statement at least once. The statement is executed first and then the conditional expression is evaluated to decide upon further iteration. Thus, the body of a while loop is executed zero or more times, and the body of a do-while loop is executed one or more times.
Style note. It is good form to always put braces around the do-while body, even when it consists of only one statement. This prevents the while part from being mistaken for the beginning of a while loop.
The following code example takes a non-negative integer val and prints it in reverse order. The use of a do-while means that 0 can be printed without needing extra special-case code.
do
{
printf("%d", val % 10);
val /= 10;
} while (val != 0);
3.6 For Loops
The for loop has the general form
for (expr1; expr2; expr3)
statement;
Its behaviour is equivalent to a while loop with the following arrangement of expressions.
expr1;
while (expr2) {
statement;
expr3;
}
In the for loop, expressions 1, 2, and 3 are optional, although the semicolons must remain. If expressions 1 or 3 are not there, then the loop simple behaves like the while loop above without expressions 1 or 3. If expression 2 is omitted, then the conditional is always TRUE, and an infinite loop results.
for (;;) /* infinite loop */
statement;
Note. It is possible to stack several expressions in the various parts of the for loop using the comma operator.The comma operator enables many statements to appear in the form of a statement, without them being attached to the braces. However, it should be used sparingly, and is most suited for situations like the following example. This example reverses a character string in-place. The first loop finds the end of the string, and the second loop performs the reversing operation by swapping characters.
for (j=0; str[j] != ’\0’; ++j)
;
for (i=0, --j; i < j; ++i, --j) {
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
3.7 Break and Continue
As seen previously, break can be used to branch out of a switch-statement. It may also be used to branch out of any of the iterative constructs. Thus, a break may be used to terminate the execution of a switch, while, do-while, or for. It is important to realise that a break will only branch out of an inner-most enclosing block, and transfers program-flow to the first statement following the block. For example, consider a nested while-loop,
while (expression) {
while (expression) {
if (expression)
break;
statements
}
statements
}
Here the break will terminate the inner while-loop and proceed to execute the statements of the outer while-loop.
This next example (adapted from [HS95, page 253]) shows a fast technique for finding the smallest element in an array of length SIZE. A break is used to terminate the infinite outer while-loop.
i = SIZE;
temp = smallest = array[0];
while (1) {
while (array[--i] > smallest)
;
if (i == 0) break;
array[0] = smallest = array[i];
}
array[0] = temp;
The continue-statement operates on loops only; it does not operate on switch. For while and do-while it causes transfer of program-control immediately to the conditional test, which is reevaluated for the next iteration. The same action occurs for a for-loop after first executing the
increment expression (i.e., expression 3). Note that, as with break, continue acts on the inner-most enclosing block of a nested loop.
The continue statement is often used when the part of the loop that follows is compli-cated, so that reversing a test and indenting another level would nest the program too deeply [KR88, page 65].
The following example shows the outline of a code-segment that performs operations on the positive elements of an array but skips the negative elements. The continue provides a concise means for ignoring the negative values.
for (i = 0; i<SIZE ; ++i) {
if (array[i] < 0) /* skip -ve elements */
continue;
/* process +ve elements */
}
Note. Both break and continue have no e ect on an if-statement. A common misconception is that break can be used to jump out of an if compound statement. For example, given a nested construct of the form,
while (expression) {
statements
if (expression) {
statements
if (expression)
break;
statements
}
statements after if
}
statements after loop
it is commonly presumed that the break will transfer control to the statements after if, whereas it will actually transfer control to the statements after loop.
No comments:
Post a Comment