11.07.2015 Views

tYSR20

tYSR20

tYSR20

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.

Chapter 4: Performing Logical Operations 51The special value endl inserts a newline. The difference between the valueendl and the character ‘\n’ as described in Chapter 2 is subtle and explainedin Chapter 24.Using logical int variablesC++ hasn’t always had a bool type variable. Back in the old days (before thatguy on TV kept walking around saying “Can you hear me now?”), C++ usedint variables to store logical values. A value of 0 was considered false andall other values true. By the same token, a logical operator generated a 0 forfalse and a 1 for true. (Thus, 0 was false while 10 > 5 returned a 1.)C++ retains a high degree of compatibility between bool and int in orderto support the older programs that still have that quirk. You get completelydifferent output from the BitTest program if you remove the line cout.setf(cout.boolalpha):Input value 1: 5Input value 2: 5The statement, 5 equals 5 is 1Press any key to continue . . .Variables of type int and bool can be mixed in expressions as well. For example,Dev-C++ allows the following bizarre statement without batting an eyelid:int n;n = nArg1 == nArg2;Continue to use type bool to hold logical values despite this wart that modernC++ inherits from its forefathers. Other compilers may not be as forgiving.Be careful performing logical operationson floating-point variablesReal numbers are those numbers that can have a fractional part. Because ofthis, real numbers cannot be counting numbers. That is, you can say the first(1st), second (2nd), third, fourth, and so on because the relationship of 1, 2,and 3 are known exactly. It does not make sense to speak of the 4.5th numberin a sequence. (This brings to mind the number between the fourth and fifth,but it has no real meaning.)Similarly the C++ type float, which is the C++ representation, is not a countingnumber. Even worse (unlike a real number), a floating-point number can’thave an infinite number of digits beyond the decimal point if a computer is

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

Saved successfully!

Ooh no, something went wrong!