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.

36 Part I: Introduction to C++ ProgrammingAre These Calculations Really Logical?C++ provides a logical variable called bool. The type bool comes from Boolean,the last name of the inventor of the logical calculus. There are two values for aboolean variable: true and false.There are actually calculations that result in the value bool. For example, “xis equal to y” is either true or false.Mixed Mode ExpressionsC++ allows you to mix variable types in a single expression. That is, you areallowed to add an integer with a double precision floating-point value. In thefollowing expression, for example, nValue1 is allowed to be an int:// in the following expression the value of nValue1// is converted into a double before performing the// assignmentint nValue1 = 1;nValue1 + 1.0;An expression in which the two operands are not the same type is called amixed-mode expression. Mixed-mode expressions generate a value whose typeis equal to the more capable of the two operands. In this case, nValue1 is convertedto a double before the calculation proceeds. Similarly, an expression ofone type may be assigned to a variable of a different type, as in the followingstatement:// in the following assignment, the whole// number part of fVariable is stored into nVariabledouble dVariable = 1.0;int nVariable;nVariable = dVariable;You can lose precision or range if the variable on the left side of the assignmentis smaller. In the previous example, C++ truncates the value of dVariablebefore storing it in nVariable.Converting a larger value type into a smaller value type is called demotion,whereas converting values in the opposite direction is known as promotion.Programmers say that the value of int variable nVariable1 is promoted to adouble as expressions such as the following:int nVariable1 = 1;double dVariable = nVariable1;

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

Saved successfully!

Ooh no, something went wrong!