Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

Chapter 3: Performing Mathematical Operations

45

Using Assignment Operators

An assignment operator is a binary operator that changes the value of its left argument. The equal sign (=), a simple assignment operator, is an absolute necessity in any programming language. This operator puts the value of the right-hand argument into the left argument. The other assignment operators are odd enough that they seem to be someone’s whim.

The creators of C++ noticed that assignments often follow the form of

variable = variable # constant

where # is some binary operator. Thus, to increment an integer operator by 2, the programmer might write

nVariable = nVariable + 2;

This expression says, “add two to the value of nVariable and store the results back into nVariable.” Doing so changes the value of nVariable to 2 more than it was.

It’s common to see the same variable on both the right and left side of an assignment.

Because the same variable appears on both sides of the = sign, the same Fathers of the C++ Republic decided to create a version of the assignment operator in which a binary operator is attached. This says, in effect, “Thou shalt perform whatever operation on a variable and store the results right back into the same variable.”

Every binary operator has one of these nifty assignment versions. Thus, the assignment just given could have been written this way:

nVariable = nVariable + 2;

nVariable += 2;

Here the first line says (being very explicit now) “Take the value of nVariable, add 2, and store the results back into nVariable.” The line is a second form if the same expression, saying (a bit more abruptly), “Add 2 to the value of nVariable.”

Other than assignment itself, these assignment operators are not used all that often. However, as odd as they might look, sometimes they can actually make the resulting program easier to read.

46

Part I: Introduction to C++ Programming

Chapter 4

Performing Logical Operations

In This Chapter

Using sometimes-illogical logical operators

Defining logical variables

Operating with bitwise logical operators logically, a bit at a time

The most common statement in C++ is the expression. Most expressions involve the arithmetic operators such as addition (+), subtraction () and

multiplication (*). This chapter describes these types of expressions.

There is a whole other class of operators known as the logical operators. In comparison with the arithmetic operators, most people don’t think nearly as much about operations.

It isn’t that people don’t deal with logical operations — after all, people compute AND and OR constantly. I won’t eat cereal unless the bowl contains cereal AND the bowl has milk in it AND the cereal is coated with sugar (lots of 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 write them down as machine instructions (or think of them in that light).

Logical operators fall into two types. The AND and OR operators are what I will call simple logical operators. There is a second type of logical operator that people don’t use in their daily business — the bitwise operator — that’s unique to the computer world. We’ll start with the simple and sneak up on the bitwise here.

Why Mess with Logical Operations?

C++ programs have to make decisions. A program that can’t make decisions is of limited use. The temperature-conversion program laid out in Chapter 1 is about as complex you can get without some type of decision-making. Invariably a computer program gets to the point where it has to figure out situations such as “Do this if the a variable is less than some value, do that other thing if it’s not.” That’s what makes a computer appear to be intelligent — that it can make