20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Program 6.10<br />

}<br />

Cont<strong>in</strong>ued<br />

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

return 0;<br />

Program 6.10 Output<br />

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47<br />

Several po<strong>in</strong>ts are worth not<strong>in</strong>g about the program <strong>in</strong> Program 6.10.The outermost for<br />

statement sets up a loop to cycle through the <strong>in</strong>tegers 2 through 50.The loop variable p<br />

represents the value you are currently test<strong>in</strong>g to see if it is prime.The first statement <strong>in</strong><br />

the loop assigns the value 1 to the variable isPrime.The use of this variable will become<br />

apparent shortly.<br />

A second loop is set up to divide p by the <strong>in</strong>tegers from 2 through p–1. Inside the<br />

loop, a test is made to see if the rema<strong>in</strong>der of p divided by d is 0. If it is, you know that p<br />

cannot be prime because an <strong>in</strong>teger other than 1 and itself can evenly divide it.To signal<br />

that p is no longer a candidate as a prime number, the value of the variable isPrime is<br />

set equal to 0.<br />

When the <strong>in</strong>nermost loop f<strong>in</strong>ishes execution, the value of isPrime is tested. If its<br />

value is not equal to zero, no <strong>in</strong>teger was found that evenly divides p; therefore, p must<br />

be a prime number, and its value is displayed.<br />

You might have noticed that the variable isPrime takes on either the value 0 or 1,<br />

and no other values.That’s why you declared it to be a _Bool variable. Its value is 1 as<br />

long as p still qualifies as a prime number. But as soon as a s<strong>in</strong>gle even divisor is found,<br />

its value is set to 0 to <strong>in</strong>dicate that p no longer satisfies the criteria for be<strong>in</strong>g prime.<br />

Often, variables that are used <strong>in</strong> such a manner are referred to as flags.A flag typically<br />

assumes only one of two different values. Furthermore, the value of a flag is usually tested<br />

at least once <strong>in</strong> the program to see if it is “on” (TRUE) or “off” (FALSE), and some<br />

particular action is taken based upon the results of the test.<br />

In C, the notion of a flag be<strong>in</strong>g TRUE or FALSE is most naturally translated <strong>in</strong>to the<br />

values 1 and 0,respectively. So <strong>in</strong> the Program 6.10, when you set the value of isPrime<br />

to 1 <strong>in</strong>side the loop, you are effectively sett<strong>in</strong>g it as TRUE to <strong>in</strong>dicate that p “is prime.”<br />

If dur<strong>in</strong>g the course of execution of the <strong>in</strong>ner for loop an even divisor is found, the<br />

value of isPrime is set to FALSE to <strong>in</strong>dicate that p no longer “is prime.”<br />

It is no co<strong>in</strong>cidence that the value 1 is typically used to represent the TRUE or “on”<br />

state and 0 to represent the FALSE or “off” state.This representation corresponds to the<br />

notion of a s<strong>in</strong>gle bit <strong>in</strong>side a computer.When the bit is “on,” its value is 1; when it is

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

Saved successfully!

Ooh no, something went wrong!