Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 18

Do C While You Sleep

In This Chapter

Introducing the do-while loop

Using a delay loop

Nesting loops

Creating a grid

Understanding the continue keyword

You’re not done looping!

You’re not done looping!

You’re not done looping!

You’re not done looping!

The for and while commands may be an odd couple, and they may be the offi­ cial looping keywords of the C language, but that’s not the end of the story. No, there’s more than one way to weave a loop, particularly a while loop. This chapter covers this oddity of nature, even showing you a few situations where the mythical “upside-down” while loop becomes a necessity.

The Down-Low on Upside-Down do-while Loops

A while loop may not repeat — no, not ever. If the condition it examines is false before the loop starts, the block of statements designed to repeat is skipped over like so many between-meal snacks would be if you really knew what’s in them.

Witness the cold, cruel while statement:

while(v==0)

226 Part III: Giving Your Programs the Ability to Run Amok

If v doesn’t equal 0, the statements clinging to the underside of the while loop are skipped, just as though you had written this:

if(v==0)

There’s an exception, of course — a kind of loop you can fashion that always executes at least once. It’s the upside-down while loop, called a do-while loop. This type of loop is rare, but it has the charming aspect of always want­ ing to go through with it once.

The devil made me do-while it!

The following program contains a simple do-while loop that counts backward. To add some drama, you supply the number it starts at:

/* An important program for NASA to properly launch America’s spacecraft. */

#include <stdio.h>

int main()

{

int start;

printf(“Please enter the number to start\n”); printf(“the countdown (1 to 100):”); scanf(“%d”,&start);

/* The countdown loop */

do

{

printf(“T-minus %d\n”,start); start--;

}

while(start>0);

printf(“Zero!\nBlast off!\n”); return(0);

}

Type this source code into your editor. Save the file to disk as COUNTDWN.C.

Compile. Fix any errors. Notice that a semicolon is required after the end of the do-while loop. If you forget it, you get an error.

Chapter 18: Do C While You Sleep 227

Run the program.

Please enter the number to start

the countdown (1 to 100):

Be a traditionalist and type 10. Press Enter:

T-minus 10

T-minus 9

et cetera . . .

T-minus 1

Zero!

Blast off!

The do while loop executes once, no matter what.

In a do-while loop, a semicolon is required at the end of the monster, after while’s condition (see Line 21 in the program).

Don’t forget the ampersand (&), required in front of the variable used in a scanf statement. Without that & there, the program really screws up.

do-while details

A do-while loop has only one advantage over the traditional while loop: It always works through once. It’s as though do is an order: “Do this loop once, no matter what.” It’s guaranteed to repeat itself. If it doesn’t, you’re entitled to a full refund; write to your congressman for the details.

The do keyword belongs to while when the traditional while loop stands on its head. The only bonus is that the while loop always works through once, even when the condition it examines is false. It’s as though the while doesn’t even notice the condition until after the loop has wended its way through one time.

Here’s the standard format thing:

do

{

statement(s);

}

while(condition);

228 Part III: Giving Your Programs the Ability to Run Amok

The condition is a true-or-false comparison that while examines — the same type of deal you find in an if comparison. If the condition is true, the statements in the loop are repeated. They keep doing so until the condition is false, and then the program continues. But, no matter what, the statements are always gone through once.

An important thing to remember here is that the while at the end of the loop requires a semicolon. Mess it up and it’s sheer torture later to figure out what went wrong.

One strange aspect of the do-while loop is that it seriously lacks the starting, while-true, and do-this aspects of the traditional while and for loops. It has no starting condition because the loop just dives right into it. Of course, this sentence doesn’t mean that the loop would lack those three items. In fact, it may look like this:

starting; do

{

statement(s); do_this;

}

while(while_true);

Yikes! Better stick with the basic while loop and bother with this jobbie only when something needs to be done once (or upside down).

The condition that while examines is either TRUE or FALSE, according to the laws of C, the same as a comparison made by an if statement. You can use the same symbols used in an if comparison, and even use the logical doodads (&& or ||) as you see fit.

This type of loop is really rare. It has been said that only a mere 5 percent of all loops in C are of the do-while variety.

You can still use break to halt a do-while loop. Only by using break, in fact, can you halt the statements in the midst of the loop. Otherwise, as with a while or for loop, all the statements within the curly braces repeat as a single block.

A flaw in the COUNTDWN.C program

Run the COUNTDWN program again. When it asks you to type a number, enter 200.

There isn’t a problem with this task; the program counts down from 200 to 0 and blasts off as normal. But, 200 is out of the range the program asks you to type.