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

Chapter 16: C the Loop, C the Loop++ 205

Quit pondering and type.

Save the file to disk as OLLYOLLY.C. Compile it. Run it.

The output looks like this:

10

9

8

7

6

5

4

3

2

1

Ready or not, here I come!

Yes, indeed, the computer can count backward. And, it did it in a for loop with what looks like a minimum of fuss — but still a little techy. The following sec­ tion discusses the details.

To prove that you’re not going crazy, refer to Chapter 15’s 100.C program. It counted from 1 to 100 using a for loop. The only difference now, aside from the printf() statements, is that the loop worked itself backward.

Backward-counting loops in C are rare. Most of the time, whenever you have to do something 10 times, you do it forward, from 0 through 9 or from 1 through 10 or however you set up your for loop.

O, to count backward

Counting backward or forward makes no difference to the computer. You just have to tell the C language in which direction you want to go.

To count forward, you increment a variable’s value. So, if you have the vari­ able f, you do this:

f=f+1;

or even this:

f++;

Either way, the value of variable f is 1 greater than it was before; it has been incremented.

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

To count backward, you subtract 1 from a variable’s value, which is exactly the way you do it in your head: 10, 9, 8, 7, and so on. It looks identical to the incrementing statement, except for the minus sign:

b=b-1;

The value of variable b is 1 less than it was before. If b came in with a value of 5, this statement sets b’s value to 4. This process is known as decrementing a variable’s value.

Decrementing, or subtracting 1 (or any number) from a variable’s value is just common subtraction. The only big deal here is that decrementing is done in a loop, which makes the loop count backward.

Incrementing means adding (1) to a variable’s value.

Decrementing means subtracting (1) from a variable’s value.

Decrementing works because C first figures out what’s on the right side of the equal sign:

b=b-1;

First comes b-1, so the computer subtracts 1 from the value of variable b.

Then, that value is slid through the equal signs, back into the variable b.

The variable is decremented.

How counting backward fits into the for loop

Take another look at Line 7 from the OLLYOLLY.C program:

for(count=10;count>0;count=count-1)

It’s basic for loop stuff. The loop has a starting place, a while-true condition, and a do-this thing. The parts are listed in Table 16-1.

Table 16-1

How the for Loop Counts Backward

Loop Part

Condition

Starting

count=10

 

 

While-true

count>0

 

 

Do-this

count=count-1

 

 

Chapter 16: C the Loop, C the Loop++ 207

Everything in the backward-counting for loop works per specifications. The loop begins by setting the value of the count variable equal to 10. And, it loops as long as the value of the count variable is greater than 0 (count>0). But, each time the loop repeats, the value of the count variable is decremented. It works backward.

Again, you have no reason to loop backward — except that the printf() state­ ment belonging to the loop displays the numbers 10 through 1 in a countdown manner. Normally (which means about 99 percent of the time), you only loop forward. It’s not only easier to do in your head, but it’s also less likely to be a source for programming boo-boos than when you try to loop backward.

Most loops count forward. The backward-counting loop in C is possible but rarely used, mostly because counting is counting and it’s just easier (for humans) to do it forward.

You can’t do a backward loop without decrementing the loop’s variable.

Other than decrementing the loop’s variable, the only difference is in the loop’s while-true condition (the one in the middle). That requires a little more mental overhead to figure out than with normal for loops. It’s another reason that this type of loop is rare.

Okay. The question arises: “Why bother?” Because you have to know about decrementing and the cryptic -- operator, covered in the next section.

Cryptic C operator symbols, Volume II:

The dec operator (--)

Just as C has a shortcut for incrementing a variable’s value, there is also a shortcut for decrementing a variable’s value. (If you read the first half of this chapter, you saw this coming from a mile back, most likely.) As a quick review, you can add 1 to a variable’s value — increment it — by using the following C language statement:

i=i+1;

Or, if you’re cool and remember the shortcut, you can use the cryptic ++ opera­ tor to do the same thing:

i++;

Both examples add 1 to the value of variable i.

Consider this example:

d=d-1

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

This statement subtracts 1 from the value of variable d, decrementing it. Here’s the whiz-bang, cryptomic shortcut:

d––;

The -- is the decrement operator, which subtracts 1 from a variable’s value.

Just like the incrementation operator, ++, the decrementing operator, --, works like other mathematical operators in the C language. In keeping with the theme of being cryptic, however, there is no equal sign. The -- tells the compiler to subtract 1 from the associated variable’s value and — poof! — it’s done.

I pronounce -- as “deck deck,” though I may be the only one in the world who does so. I haven’t heard anyone say “minus-minus” — at least not aloud.

The equation d-- is the same as d=d-1.

You suffer no penalty for using d=d-1 if you forget about the -- thing.

Don’t bother with an equal sign when you use the decrementing opera­ tor, --. Glue it to the end of the variable you want to decrement and you’re done.

Here we go (again):

 

var=3;

/* the value of variable var is three */

 

var--;

/* Whoa! var is decremented here */

 

 

/* Now the value of var equals two */

The -- operator is used this way in a for looping statement:

 

 

 

 

for(d=7;d>0;d--)

 

 

The d-- replaces the d=d-1, as I demonstrate in the past few sections.

A final improvement to OLLYOLLY.C

Now that you know about --, you can improve the awkward and potentially embarrassing OLLYOLLY.C program, by spiffing it up with the decrementation operator.

Load OLLYOLLY.C into your editor again (if it’s not there right now) and clickety-clack the down-arrow key to Line 7, where the for loop starts. Edit the for statement there, replacing the count=count-1 part with the cryptic, though proper, count-- thing. Here’s how that line should look when you’re done editing: