Previous | Table of Contents | Next |
The while statement is another control structure frequently used with compound statements. Heres the syntax for while:
while (expression) statement
As with if statements, the conditional expression in the while statement can be any valid integer expression. All nonzero values are interpreted as true. Comparison operators (<, >, ==, <=, =>, and !=) return true (1) or false (0).
For example, the following code counts down from 5 to 1. The while statement executes the statement block repeatedly until the condition, n > 0, is false.
int n = 5; while (n > 0) { printf(%d\n, n); n = n - 1; }
The next example, which is slightly more compact, does the same thing:
int n = 5; while (n) { printf(%d\n, n); n = n - 1; }
There are a couple of ways to make this code even more compact, and one of them is introduced in the next section.
C++ has a number of fancy operators generally not found in any other language (except C). These operators include increment and decrement operators, assignment operators, and bitwise operators.
Among the easiest to use and most useful C++ operators are the increment and decrement operators. These operators simply add or subtract one from a variable. For example:
n++; // Add 1 to n. n; // Subtract 1 from n.
You can certainly use these operators inside a larger expression. So, for example, the decrement operator can be used to make the count-down-from-five example from the previous section even more compact:
while (n) printf(%d\n, n--);
Theres a catch that you need to be careful about: increment and decrement come in a prefix variety (++n) and a postfix variety (n++). A postfix expression, used in this last example, evaluates to the current value of the variable. Then the variable is incremented or decremented.
With the prefix variety (such as --n), the change is made first. The expression evaluates to the new value. Consequently, the following code counts from 4 to 0 rather than from 5 to 1. In each iteration through the loop, n is decremented before being printed.
while (n) printf(%d\n, n);
C++ assignment operators include not only the standard assignment operator (=) but also a range of other operators that combine assignment with another operation. These operators are similar to the increment and decrement operators, because they modify by performing an operation.
For example, addition-assignment (+=) performs both an addition and an assignment. The statement
n += 10;
is equivalent to the longer statement:
n = n + 10;
You can think of postfix increment and decrement operations as special cases of addition-assignment and subtraction-assignment. For example, these two statements are equivalent:
n; // Subtract 1 from n. n -= 1; // Subtract 1 from n.
C++ supports a large set of assignment operators that work in a similar way, performing an operation and assigning the result to the variable on the left side of the assignment statement. These operators include *= (multiplication-assignment), /= (division assignment), and many others. For more information, see the topic Assignment Operators in Part III.
To programmers new to C and C++, bitwise operators are often the most exciting part of the language. These operators enable you to test and modify individual bits.
A simple program has no need for such bit surgery, however, and even complex programs can usually get by without them. The major advantage of bit operations is that they make possible the most efficient use of space. Using bit operations, you can stuff data into individual fields within an integer, rather than use a series of different variables.
Table 2.3 summarizes bit-testing operations along with logical operators. C++ supports both. Logical operators are similar to the bit operators except that logical operators do not test individual bits. For example, ANDing together any two nonzero values results in the value true (1) when you use logical AND.
Table 2.3 Logical and bitwise operators. | |
Operator | Description |
---|---|
& | Bitwise AND. Sets a bit in the result if both of the corresponding bits in the two operands are set. |
I | Bitwise OR. Sets a bit in the result if either of the corresponding bits in the two operands is set. |
~ | Bitwise NOT (ones complement). Sets a bit in the result if the corresponding bit in the single operand is not set. |
&& | Logical AND. If both operands are nonzero, evaluates to true (1). Evaluates to false (0) otherwise. Combines Boolean conditions as you would expect. |
II | Logical OR. If either operand is nonzero, evaluates to true (1). Evaluates to false (0) otherwise. Combines Boolean conditions as you would expect. |
! | Logical NOT. Evaluates to true (1) if operand is zero, and false (0) if the operand is nonzero. Reverses the true/false value of a Boolean condition as you would expect. |
C++ also supports right-shift and left-shift operators, as shown in Table 2.4.
Table 2.4 Right-shift and left-shift operators. | |
Operator | Description |
---|---|
>> | Right shift |
<< | Left shift |
To understand how bit operators work, you need to understand the binary numbering system, which I dont explain in detail here. You can use bit-test and bit-shift operators on any integer, but these operators work on the data as actually stored in the computer: the data is in binary form.
Figure 2.8 Binary representation.
For example, the number 18 is stored as 00010010, and the number 6 is stored as 00000110. Using binary AND on them tests each bit against the corresponding bit in the other operand; a bit is set in the result only if both of the corresponding bits in the operands are 1. The result here is 00000010 binary, or 2.
The following program uses the left-shift operator (<<) and bitwise AND (&) to print the binary representation of an integer. Note that 0x8000 is C++ notation for hexadecimal representation. 0x8000 is the value of the short integer that has the most significant bit set (equal to 1) and all the others off (equal to 0).
#include <stdio.h> void print_binary(short input_field); void main() { short n; do { printf(\nEnter a short integer); prtinf((0 to quit): ); scanf(%hd, &n); print_binary(n); } while (n); } void print_binary (short input_field) { int i = 1, bit_set; while (i <= 16) { bit_set = ((0x8000 & input_field) > 0); printf(%d, bit_set); input_field = input_field << 1; i++; } }
This program takes advantage of the fact that, in C++, conditions evaluate to integers. Inside the while loop, 0x8000 is ANDed with the argument. The result is either 0x8000 or zero. The result is compared to zero, and the comparison expression evaluates to either 0 or 1, which is then printed:
(0x8000 & input_field) > 0
C++ supports a left-shift-and-assignment operator (<<=) that combines left shift with assignment. You can make the program more compact by using this operator.
Previous | Table of Contents | Next |