20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

76 Chapter 6 Mak<strong>in</strong>g Decisions<br />

The proper use of <strong>in</strong>dentation goes a long way toward aid<strong>in</strong>g your understand<strong>in</strong>g of the<br />

logic of complex statements.<br />

Of course, even if you use <strong>in</strong>dentation to <strong>in</strong>dicate the way you th<strong>in</strong>k a statement will<br />

be <strong>in</strong>terpreted <strong>in</strong> the C language, it might not always co<strong>in</strong>cide with the way that the<br />

compiler actually <strong>in</strong>terprets the statement. For <strong>in</strong>stance, remov<strong>in</strong>g the first else clause<br />

from the previous example<br />

if ( gameIsOver == 0 )<br />

if ( playerToMove == YOU )<br />

pr<strong>in</strong>tf ("Your Move\n");<br />

else<br />

pr<strong>in</strong>tf ("The game is over\n");<br />

does not result <strong>in</strong> the statement be<strong>in</strong>g <strong>in</strong>terpreted as <strong>in</strong>dicated by its format. Instead, this<br />

statement is <strong>in</strong>terpreted as<br />

if ( gameIsOver == 0 )<br />

if ( playerToMove == YOU )<br />

pr<strong>in</strong>tf ("Your Move\n");<br />

else<br />

pr<strong>in</strong>tf ("The game is over\n");<br />

because the else clause is associated with the last un-elsed if.You can use braces to<br />

force a different association <strong>in</strong> those cases <strong>in</strong> which an <strong>in</strong>nermost if does not conta<strong>in</strong> an<br />

else, but an outer if does.The braces have the effect of “clos<strong>in</strong>g off” the if statement.<br />

Thus,<br />

if ( gameIsOver == 0 ) {<br />

if ( playerToMove == YOU )<br />

pr<strong>in</strong>tf ("Your Move\n");<br />

}<br />

else<br />

pr<strong>in</strong>tf ("The game is over\n");<br />

achieves the desired effect, with the message “The game is over” be<strong>in</strong>g displayed if the<br />

value of gameIsOver is not 0.<br />

The else if Construct<br />

You’ve seen how the else statement comes <strong>in</strong>to play when you have a test aga<strong>in</strong>st two<br />

possible conditions—either the number is even, else it is odd; either the year is a leap<br />

year, else it is not. However, programm<strong>in</strong>g decisions that you have to make are not<br />

always so black-and-white. Consider the task of writ<strong>in</strong>g a program that displays –1 if a<br />

number typed <strong>in</strong> by a user is less than zero, 0 if the number typed <strong>in</strong> is equal to zero, and<br />

1 if the number is greater than zero. (This is actually an implementation of what is commonly<br />

called the sign function.) Obviously, you must make three tests <strong>in</strong> this case—to<br />

determ<strong>in</strong>e if the number that is keyed <strong>in</strong> is negative, zero, or positive. Our simple ifelse<br />

construct does not work. Of course, <strong>in</strong> this case, you could always resort to three

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!