Chap 02
Chap 02
Expressions
This chapter introduces the built-in C++ operators for composing expressions.
An expression is any computation which yields a value.
When discussing expressions, we often use the term evaluation. For
example, we say that an expression evaluates to a certain value. Usually the
final value is the only reason for evaluating the expression. However, in some
cases, the expression may also produce side-effects. These are permanent
changes in the program state. In this sense, C++ expressions are different from
mathematical expressions.
C++ provides operators for composing arithmetic, relational, logical,
bitwise, and conditional expressions. It also provides operators which produce
useful side-effects, such as assignment, increment, and decrement. We will
look at each category of operators in turn. We will also discuss the precedence
rules which govern the order of operator evaluation in a multi-operator
expression.
C++ provides five basic arithmetic operators. These are summarized in Table
2.1.
Except for remainder (%) all other arithmetic operators can accept a mix
of integer and real operands. Generally, if both operands are integers then the
result will be an integer. However, if one or both of the operands are reals
then the result will be a real (or double to be exact).
When both operands of the division operator (/) are integers then the
division is performed as an integer division and not the normal division we
are used to. Integer division always results in an integer outcome (i.e., the
result is always rounded down). For example:
9 / 2 // gives 4, not 4.5!
-9 / 2 // gives -5, not -4!
The remainder operator (%) expects integers for both of its operands. It
returns the remainder of integer-dividing the operands. For example 13%3 is
calculated by integer dividing 13 by 3 to give an outcome of 4 and a remainder
of 1; the result is therefore 1.
It is possible for the outcome of an arithmetic operation to be too large
for storing in a designated variable. This situation is called an overflow. The
outcome of an overflow is machine-dependent and therefore undefined. For
example:
unsigned char k = 10 * 92; // overflow: 920 > 255
C++ provides six relational operators for comparing numeric quantities. These
are summarized in Table 2.2. Relational operators evaluate to 1 (representing
the true outcome) or 0 (representing the false outcome).
Note that the <= and >= operators are only supported in the form shown.
In particular, =< and => are both invalid and do not mean anything.
The operands of a relational operator must evaluate to a number.
Characters are valid operands since they are represented by numeric values.
For example (assuming ASCII coding):
'A' < 'F' // gives 1 (is like 65 < 70)
C++ provides three logical operators for combining logical expression. These
are summarized in Table 2.3. Like the relational operators, logical operators
evaluate to 1 or 0.
C++ does not have a built-in boolean type. It is customary to use the type
int for this purpose instead. For example:
C++ provides six bitwise operators for manipulating the individual bits in an
integer quantity. These are summarized in Table 2.4.
The auto increment (++) and auto decrement (--) operators provide a
convenient way of, respectively, adding and subtracting 1 from a numeric
variable. These are summarized in Table 2.6. The examples assume the
following variable definition:
int k = 5;
Both operators can be used in prefix and postfix form. The difference is
significant. When used in prefix form, the operator is first applied and the
outcome is then used in the expression. When used in the postfix form, the
expression is evaluated first and then the operator applied.
Both operators may be applied to integer as well as real variables,
although in practice real variables are rarely useful in this form.
The assignment operator is used for storing a value at some memory location
(typically denoted by a variable). Its left operand should be an lvalue, and its
right operand may be an arbitrary expression. The latter is evaluated and the
outcome is stored in the location denoted by the lvalue.
An lvalue (standing for left value) is anything that denotes a memory
location in which a value may be stored. The only kind of lvalue we have seen
so far in this book is a variable. Other kinds of lvalues (based on pointers and
references) will be described later in this book.
The assignment operator has a number of variants, obtained by combining
it with the arithmetic and bitwise operators. These are summarized in Table
2.7. The examples assume that n is an integer variable.
The conditional operator takes three operands. It has the general form:
Note that of the second and the third operands of the conditional operator
only one is evaluated. This may be significant when one or both contain side-
effects (i.e., their evaluation causes a change to the value of a variable). For
example, in
int min = (m < n ? m++ : n++);
Multiple expressions can be combined into one expression using the comma
operator. The comma operator takes two operands. It first evaluates the left
operand and then the right operand, and returns the value of the latter as the
final outcome. For example:
int m, n, min;
int mCount = 0, nCount = 0;
//...
min = (m < n ? mCount++, m : nCount++, n);
Here when m is less than n, mCount++ is evaluated and the value of m is stored
in min. Otherwise, nCount++ is evaluated and the value of n is stored in min.
C++ provides a useful operator, sizeof, for calculating the size of any data
item or type. It takes a single operand which may be a type name (e.g., int) or
an expression (e.g., 100) and returns the size of the specified entity in bytes.
The outcome is totally machine-dependent. Listing 2.1 illustrates the use of
sizeof on the built-in types we have encountered so far.
Listing 2.1
1 #include <iostream.h>
11 cout << "1.55 size = " << sizeof(1.55) << " bytes\n";
12 cout << "1.55L size = " << sizeof(1.55L) << " bytes\n";
13 cout << "HELLO size = " << sizeof("HELLO") << " bytes\n";
14 }
When run, the program will produce the following output (on the author’s
PC):
char size = 1 bytes
char* size = 2 bytes
short size = 2 bytes
int size = 2 bytes
long size = 4 bytes
float size = 4 bytes
double size = 8 bytes
1.55 size = 8 bytes
1.55L size = 10 bytes
HELLO size = 6 bytes
For example, in
a == b + c * d
c * d is evaluated first because * has a higher precedence than + and ==. The
result is then added to b because + has a higher precedence than ==, and then
== is evaluated. Precedence rules can be overridden using brackets. For
example, rewriting the above expression as
a == (b + c) * d
A value in any of the built-in types we have see so far can be converted (type-
cast) to any of the other types. For example:
(int) 3.14 // converts 3.14 to an int to give 3
(long) 3.14 // converts 3.14 to a long to give 3L
(double) 2 // converts 2 to a double to give 2.0
(char) 122 // converts 122 to a char whose code is 122
(unsigned short) 3.14 // gives 3 as an unsigned short
2.2 Add extra brackets to the following expressions to explicitly show the order in
which the operators are evaluated:
(n <= p + q && n >= p - q || n == 0)
(++n * q-- / ++p - q)
(n | p & q ^ p << 2 + q)
(p < q ? n < p ? q * n - 2 : q / n + 1 : q - n)
2.3 What will be the value of each of the following variables after its initialization:
double d = 2 * int(3.14);
long k = 3.14 - 3;
char c = 'a' + 2;
char c = 'p' + 'A' - 'a';
2.4 Write a program which inputs a positive integer n and outputs 2 raised to the
power of n.
2.5 Write a program which inputs three numbers and outputs the message Sorted
if the numbers are in ascending order, and outputs Not sorted otherwise.