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 4Performing Logical OperationsIn This Chapter Using sometimes-illogical logical operators Defining logical variables Operating with bitwise logical operators logically, a bit at a timeThe most common statement in C++ is the expression. Most expressionsinvolve the arithmetic operators such as addition (+), subtraction (–) andmultiplication (*). This chapter describes these types of expressions.There is a whole other class of operators known as the logical operators. Incomparison with the arithmetic operators, most people don’t think nearly asmuch about operations.It isn’t that people don’t deal with logical operations — after all, peoplecompute AND and OR constantly. I won’t eat cereal unless the bowl containscereal AND the bowl has milk in it AND the cereal is coated with sugar (lotsof sugar). I’ll have a Scotch IF it’s single-malt AND someone else paid for it.People use such logical operations all the time, it’s just that they don’t writethem down as machine instructions (or think of them in that light).Logical operators fall into two types. The AND and OR operators are what Iwill call simple logical operators. There is a second type of logical operatorthat people don’t use in their daily business — the bitwise operator — that’sunique to the computer world. We’ll start with the simple and sneak up onthe bitwise here.Why Mess with Logical Operations?C++ programs have to make decisions. A program that can’t make decisions isof limited use. The temperature-conversion program laid out in Chapter 1 isabout as complex you can get without some type of decision-making. Invariablya computer program gets to the point where it has to figure out situations suchas “Do this if the a variable is less than some value, do that other thing if it’snot.” That’s what makes a computer appear to be intelligent — that it can make

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

Saved successfully!

Ooh no, something went wrong!