Operators and Shortcuts

So far, you have met only the basic operators for addition, multiplication, and assignment. C++ has a very rich set of operators, including several designed to manipulate the individual bits of numbers directly and efficiently. I won't do those immediately, but concentrate on the most commonly used operators first. C++ came from C, which was designed by people who hated unnecessary typing, so you may find the operator symbols cryptic at first. But everyone gets used to them with practice.

Division and Remainder

In C++, division is expressed by the forward slash (/). The operator / has the same precedence as the operator *, just as in ordinary arithmetic, so the parentheses are necessary in the following example:

;> double x = 1.2;
;> (x+1)/(x–1) + 1;
(double) 12.

In mixed real/integer expressions, the integers are promoted into double-precision numbers. What if you want an integer result? If you are only interested in the integer part of a division operation, you can cast a double into an int; this expression (int)x is called a typecast. Going from a double to an int is called a narrowing conversion because the result has less precision. As you can see in the following example, it happens automatically when assigning a double to an int. Most compilers warn you about narrowing conversions, so it's best to explicitly use a typecast.

;> x
							=
							2.3;
;> (double) 2.3
;> (int) x / 2;
(int) 1
;> n
							=
							x
							/
							2;
(int) 1

It is common to need the remainder after an integer division. For instance, it will be zero if a divisor can divide exactly into a number. The remainder or modulo operator (%) does this for you. An interesting use of % is with the standard function rand(), which generates a large pseudo-random number. rand() % 10 is guaranteed to be between 0 and 9:

;> 4
							%
							2;
(int) 0
;> 5
							%
							2;
(int) 1
;> rand() % 10;
(long int) 1
rand() % 10;
(long int) 7
;> rand() % 10;
(long int) 4

Logical Operations

As discussed earlier in the chapter the assignment operator (=) does not mean equality. C++ uses the == symbol to indicate equality. So you can read a == b as a equals b. Simularly, != means 'not equal to'.

;> n
							=
							1;
(int) 1
;> n
							== 1;
(bool) true
;> n
							!= 1;
(bool) false

The result of a comparison is of type bool, which is short for Boolean. (The founder of mathematical logic was named Boole.) This type was not part of the original C++ language, in which comparisons resulted in either one or zero. Generally, any nonzero result is considered true. The type bool is considered to be an integer, and the compiler converts it into a number if necessary.

There are other comparison operators as well: less than (<), greater than (>), less than or equal (<=), and greater than or equal (>=). These can be combined together with && and ||, which mean and and or respectively. You express not by using the operator ! and not equal by using the operator !=.


;> 20 > 10 && n == 1;
(bool) true
;> n
							>
							1
							|| n != 5;
(bool) true
;> n
							== 1+2 && 20 > 10 || n==5;
(bool) false

C++ notation takes some practice to get right. For example, note the respective precedence of && and || in the third expression in the preceding example:

;> n
							== 1+2 && 20 > 10 || n==5;
						

&& is evaluated first, and then || is evaluated; the + operator has highest precedence of all. The Pascal equivalent would be expressed as follows:

((n = 1+2) and (20 > 10)) or (n = 5)

If you are unsure of the exact operator precedence in a C++ expression, there is no harm in adding extra parentheses. They have no effect on the resulting code, except that they make sure that it says exactly what you wanted.

Shortcuts

C++ inherited from C some operators (such as ++, , +=, -=, and *=) that increment and decrement variables directly, rather than forcing you to use expressions such as n = n+1. If n is an integer, then both ++n and n++ increment that integer; they differ in the value of the expression. ++n is called the prefix increment operator, and n++ is the postfix increment operator.


;> int nn = 1;
;> ++nn;
(int) 2
;> nn;
 (int) 2
;> nn++;
(int) 2
;> nn;
(int) 3

The prefix operator returns the value after the increment, and the postfix operator returns the value before the increment. There are also two decrement operators, nn— and —nn, which work similarly to ++n and n++.

A number of assignment expressions apply an operation to the right-hand side of an equation. For instance, it is common to have statements such as nn = nn + 2. Although this is a valid C++ expression, in C++ this statement can be written as nn += 2. The operators -= and *= can be used similarly. The following code shows these shortcuts in use, assuming that nn was originally 2. Their value in each case is the value of nn after the operation.

;> nn += 2;
(int) 5
;> nn -= 2;
(int) 3
;> nn *= 4;
(int) 12

Generally, the shortcuts are not only easier to type, but produce faster code than writing them out in full (like n=n+2). This is part of the original C philosophy of being a fast language that's quick to type. The code n=n+1 is not incorrect, but you will rarely see it in well-written C++ code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.163.13