|
|
|
|
|
|
|
Some people make a game of seeing how much they can do in as few keystrokes as possible. But they should remember that serious software development requires writing code that other programmers can read and understand. Overuse of side effects hinders this goal. By far, the most common use of the ++ and -- operators is to do the incrementation or decrementation as a separate expression statement: |
|
|
|
|
|
|
|
|
Here, the value of the expression is unused, but we get the desired side effect of incrementing count. In this example, it doesn't matter whether we use pre-incrementation or post-incrementation. The choice is up to you. |
|
|
|
|
|
|
|
|
The bitwise operators listed in the operator table (<, >>, &, |, and so forth) are used for manipulating individual bits within a memory cell. This book does not explore the use of these operators; the topic of bit-level operations is beyond an introduction to computer science and computer programming. However, we point out two things about the bitwise operators. |
|
|
|
|
|
|
|
|
First, the built-in operators < and >> are the left shift and right shift operators, respectively. Their purpose is to take the bits within a memory cell and shift them to the left or right. Of course, we have been using these operators all along, but in an entirely different contextprogram input and output. The header file iostream.h uses an advanced C++ technique called operator overloading to give additional meanings to these two operators. An overloaded operator is one that has multiple meanings, depending on the data types of its operands. Looking at the < operator, the compiler determines by context whether a left shift operation or an output operation is desired. Specifically, if the first (left-hand) operand denotes an output stream, then it is an output operation. If the first operand is an integer variable, it is a left shift operation. |
|
|
|
|
|
|
|
|
Second, we repeat our caution from Chapter 5: Do not confuse the && and || operators with the & and | operators. The statement |
|
|
|
|
|
|
|
|
if (i == 3 & j == 4) // Wrong
k = 20; |
|
|
|
|
|
|
|
|
is syntactically correct because & is a valid operator (the bitwise AND operator). The program containing this statement compiles correctly but executes incorrectly. Although we do not examine what the bitwise AND and OR operators do, just be careful to use the relational operators && and || in your logical expressions. |
|
|
|
|
|