Program Looping in C: The do Statement

The two looping statements discussed so far in this chapter both make a test of the con- ditions before the loop is executed. Therefore, the body of the loop might never be exe- cuted at all if the conditions are not satisfied. When developing programs, it sometimes becomes desirable to have the test made at the end of the loop rather than at the begin- ning. Naturally, the C language provides a special language construct to handle such a situation. This looping statement is known as the do statement. The syntax of this state- ment is as follows:

do

program statement

while ( loop_expression );

Execution of the do statement proceeds as follows: the program statement is executed first. Next, the loop_expression inside the parentheses is evaluated. If the result of eval- uating the loop_expression is TRUE, the loop continues and the program statement is once again executed. As long as evaluation of the loop_expression continues to be TRUE, the program statement is repeatedly executed. When evaluation of the expres-sion proves FALSE, the loop is terminated, and the next statement in the program is exe- cuted in the normal sequential manner.

The do statement is simply a transposition of the while statement, with the looping conditions placed at the end of the loop rather than at the beginning.

Remember that, unlike the for and while loops, the do statement guarantees that the body of the loop is executed at least once.

In Program 5.8, you used a while statement to reverse the digits of a number. Go back to that program and try to determine what would happen if you typed in the number 0 instead of 13579. The loop of the while statement would never be executed and you would simply end up with a blank line in your display (as a result of the display of the newline character from the second printf statement). If you use a do statement instead of a while statement, you are assured that the program loop executes at least once, thus guaranteeing the display of at least one digit in all cases. Program 5.9 shows this revised program.

Program 5.9   Implementing  a Revised  Program to Reverse the Digits  of a Number

// Program to reverse the digits of a number

#include <stdio.h>

int main ()

{

int number, right_digit;

printf (“Enter your number.\n”);

scanf (“%i”, &number);

do {

right_digit = number % 10;

printf (“%i”, right_digit);

number = number / 10;

}

while ( number != 0 );

printf (“\n”);

return 0;

}

Program 5.9   Output

Enter your number.

13579

97531

Program 5.9   Output (Rerun)

Enter your number.

0

0

As you can see from the program’s output, when 0 is keyed into the program, the pro- gram correctly displays the digit 0.

1. The break Statement

Sometimes when executing a loop, it becomes desirable to leave the loop as soon as a certain condition occurs (for instance, you detect an error condition, or you reach the end of your data prematurely). The break statement can be used for this purpose. Execution of the break statement causes the program to immediately exit from the loop it is executing, whether it’s a for, while, or do loop. Subsequent statements in the loop are skipped, and execution of the loop is terminated. Execution continues with whatever statement follows the loop.

If a break is executed from within a set of nested loops, only the innermost loop in which the break is executed is terminated.

The format of the break statement is simply the keyword break followed by a semi- colon:

break;

2. The continue Statement

The continue statement is similar to the break statement except it doesn’t cause the loop to terminate. Rather, as its name implies, this statement causes the loop in which it is executed to be continued. At the point that the continue statement is executed, any statements in the loop that appear after the continue statement are automatically skipped. Execution of the loop otherwise continues as normal.

The continue statement is most often used to bypass a group of statements inside a loop based upon some condition, but to otherwise continue execution of the loop. The format of the continue statement is simply

continue;

Don’t use the break or continue statements until you become very familiar with writ- ing program loops and gracefully exiting from them. These statements are too easy to abuse and can result in programs that are hard to follow.

Now that you are familiar with all the basic looping constructs provided by the C language, you are ready to learn about another class of language statements that enable you to make decisions during the execution of a program. These decision-making capa- bilities are described in detail in Chapter 6, “Making Decisions.” First, try the exercises that follow to be certain you understand how to work with loops in C.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *