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

Chapter 18: Do C While You Sleep 235

This program creates a 10-by-9 square (okay — grid) of numbers and letters by using a nested-loop arrangement.

Enter the source code into your editor. Save your efforts to disk as GRID.C.

Compile the program. Notice that putting two for statements together doesn’t cause the compiler to spew errors at you (unless you made a typo somewhere).

Run. Here’s what the output should look like:

Here is thy grid...

1-A 1-B 1-C 1-D 1-E 1-F 1-G 1-H 1-I 1-J 2-A 2-B 2-C 2-D 2-E 2-F 2-G 2-H 2-I 2-J 3-A 3-B 3-C 3-D 3-E 3-F 3-G 3-H 3-I 3-J 4-A 4-B 4-C 4-D 4-E 4-F 4-G 4-H 4-I 4-J

5-A 5-B 5-C 5-D 5-E 5-F 5-G 5-H 5-I 5-J 6-A 6-B 6-C 6-D 6-E 6-F 6-G 6-H 6-I 6-J 7-A 7-B 7-C 7-D 7-E 7-F 7-G 7-H 7-I 7-J 8-A 8-B 8-C 8-D 8-E 8-F 8-G 8-H 8-I 8-J 9-A 9-B 9-C 9-D 9-E 9-F 9-G 9-H 9-I 9-J

Wow. Such efficiency should please any government bureaucracy.

The first, outer for loop counts from 1 to 10.

The inner for loop may seem strange, but it’s not. It’s only taking advan­ tage of the dual-number/-character nature of letters in a computer. The character variable b starts out equal to the letter A and is incremented one letter at a time, up to the letter K. What happens is that b is set equal to the letter A’s ASCII value, which is 65. The variable b then increments up to the letter K’s ASCII value, which is 75. It’s sneaky, but doable.

The printf() function displays the numbers and letters as the inner loop spins. You can see this process on your screen: The outer loop stays at one number while the letters A through K are printed. Then, the outer loop is incremented, and the next row of letters is printed.

Note that the printf() function has a space after the %c character. That’s what keeps the columns in the grid from running into each other.

The putchar() function displays a single character on the screen. In GRID.C, it’s used to display a \n newline character at the end of each row.

Break the Brave and Continue the Fool

Two C language keywords can be used to directly control loops in your pro­ grams. The keywords are break and continue. The break keyword should be familiar to you, having been introduced in Chapter 15 and tossed at you every now and again since then. The continue keyword is a new beast, but it plays an important role — one that you may even find handy.

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

What continue does is to instantly repeat a loop. It works like break in that the rest of the statements in the loop are skipped; unlike break, the continue command sends the computer back to the beginning of the loop. The fool!

Both break and continue are C language keywords and statements unto themselves. They each must end with a semicolon.

Both break and continue work inside any C loop.

Both break and continue cause horrid errors outside a loop.

Please continue. . . .

The following program is BORC.C, which isn’t named after the Muppet’s Swedish Chef. Instead, it’s an acronym for break or continue, the two key­ words this program fiddles with:

#include <stdio.h>

int main()

{

int x=0;

for(;;)

{

x++;

if(x<=5)

{

printf(“%d, “,x); continue;

}

printf(“%d is greater than 5!\n”,x); break;

}

return(0);

}

Type the source code for BORC.C into your editor. Save it to disk as BORC.C, which stands for break or continue. (If you keep thinking of Bork-Bork-Bork, you save it to disk as BORK.C, which isn’t what I’m after.)

Compile and run the program.

Here’s how it goes:

1, 2, 3, 4, 5, 6 is greater than 5!

Chapter 18: Do C While You Sleep 237

The BORC.C program contains an endless for loop. Furthermore, the loop is cut short by the continue statement. After the value of x grows to be more than 5, the continue is skipped and, finally, a break statement stops the end­ less loop.

The for(;;) part indicates an endless loop. I read it as “for ever.”

Notice the space after the %d and comma in the printf() statement.

The continue statement causes the rest of the loop — the printf() and break — to be skipped, and then the loop is repeated. The increasing value of x proves that the loop continues to spin.

An else condition doesn’t have to be present to complement the if in Line 10. That’s because continue halts the program right then and there! If no continue were present, the break in Line 16 would ensure that the loop ran through only once.

The continue keyword

Like the break keyword, the continue keyword is used to affect how a loop loops. This time, the job is to immediately repeat the loop, skipping over the remaining statements and starting the loop over with the first line (the for, while, or do or whatever started the loop in the first place):

continue;

The continue keyword is a C language statement unto itself and must end properly with a semicolon.

The continue command comes in handy when you have statements in a loop that you don’t want to repeat every time; continue just skips over them and starts the loop over at the beginning.

The continue command works in any loop, not just in while loops.

Like break, continue affects only the loop it’s in.

The continue keyword forces the computer to instantly repeat a loop. The rest of the statements in the loop are just skipped, and the loop starts all over.

Note that continue repeats only the loop it’s in. If you have a nested loop, keep in mind that continue affects only one loop at a time. Also, continue cannot repeat a while loop when the loop’s condition becomes false.

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

Keep in mind that although continue forces another spin of the loop’s wheel, it doesn’t reinitialize the loop. It tells the compiler to “go again,” not to “start over.”

You should keep in mind only two real warnings about the continue command: Don’t use it outside a loop or expect it to work on nested loops; and be careful where you put it in a while loop, lest you skip over the loop’s counter and accidentally create an endless loop.

As a final, consoling point, this command is rarely used. In fact, many C programmers may be a little fuzzy on what it does or may not know pre­ cisely how to use it.