Identifiers, together with a large collection of symbols called operators, form expressions which are combined to make statements. Sequences of these statements are combined to make functions, and a collection of these functions constitutes a C++ program. We have already considered data and data types -- we will now build some elementary expressions by combining data and operators.
The C++ operators fall into three categories: (a) arithmetic, (b) relational and logical, or (c) other. C++ is a very operator-rich language, and there are a great number of operators in the "other" category. Rather than print a list of all the operators in C++, a long and very scary list, our strategy will be to (a) provide a list of those common operators a beginning programmer will encounter, and (b) introduce other operators as needed in our journey through C++.
Arithmetic Operators
The basic arithmetic operators in C++ are the same as in most other computer languages, and correspond to our usual mathematical/algebraic symbolism:
|
Operator |
Meaning |
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
The only surprise in the table above might be the modulus operator which is a division operation where two integers are divided and the remainder is the result. For example, the expression
10 % 3 results in 1,
12 % 7 results in 5, and
20 % 5 results in 0.
Integer division (an integer divided by an integer) also carries a word of caution - the result of an integer divided by an integer is always an integer (the remainder is trucated). For example,
10/3 results in 3,
14/5 results in 2, and
1/2 results in 0.
The use of the expression 1/2 for the value 0.5 has caused many beginning programmers problems because 1/2 is 0 according to integer division (1./2. or 1.0/2.0, on the other hand, does produce the value 0.5).
Examples of arithmetic expressions
pi * radius * radius
0.5 * acceleration * time * time + initialVelocity * time + initialPosition
4.0 / 3.0 * pi * radius * radius * radius
twentyFourHourTime % 12
1./2.* base * height
An interesting no-show in the list of operators in C++ is an exponentiation operator, such as BASIC’s ^ and FORTRAN’s ** operators. The ANSI Standard C++ has no exponentiation operator; raising one number to the power of another is accomplished using functions, which will be discussed later.
The Assignment Statement
The general form of an assignment statement is
identifier = expression;
The = symbol is an assignment operator rather than an equality operator. The expresion on the right is evaluated and assigned to the identifier on the left.
Example program with assignment statements
Relational Operators
Using relational operators we can direct the computer to compare two variables. The C++ relational operators are summarized below, with their meanings. Pay particular attention to the equality operator; it consists of two equal signs, not just one.
|
Operator |
Meaning |
| > | Greater than |
| >= | Greater than or equal to |
| < | Less than |
| <= | Less than or equal to |
| == | Equal |
| != | Not equal |
Examples
Relational operators usually appear in statements which are inquiring about the truth of some particular relationship between variables. Though we have not discussed the syntax of this kind of statement, interpreting the examples below should not be difficult. The relational operators are the operators in the expressions that appear between the parentheses.
if (thisNum < minimumSoFar) minimumSoFar = thisNum
if (job == Teacher) salary == minimumWage
if (numberOfLegs != 8) thisBug = insect
if (degreeOfPolynomial < 2) polynomial = linear
if (surf == up) myLocation = oughtaHere
In C++ the truth value of these expressions are assigned numerical values: a truth value of false is assigned the numerical value zero and the value true is assigned a numerical value best described as not zero.
|
|
|
|
|
| 98.7 | not zero (the expression is true) | zero (the expression is false) | zero (the expression is false) |
| 100.0 | zero (the expression is false) | not zero (the expression is true) | not zero (the expression is true) |
| 105.3 | zero (the expression is false) | zero (the expression is false) | not zero (the expression is true) |
Logical Operators
Logical operators in C++, as with other computer languages, are used to evaluate expressions which may be true or false. Expressions which involve logical operations are evaluated and found to be one of two values: true or false.
| Operator | Meaning | Example of Use | Truth Value |
|---|---|---|---|
| && | AND | (exp 1) && (exp 2) | True if exp 1 and exp 2 are BOTH true. |
| || | OR | (exp 1) || (exp 2) | True if EITHER (or BOTH) exp 1 or exp 2 are true. |
| ! | NOT | ! (exp 1) | Returns the opposite truth value of exp 1; if exp 1 is true, ! (exp 1) is false; if exp 1 is false, ! (exp 1) is true. |
Examples of expressions which contain relational and logical operators
if ((bodyTemp > 100) && (tongue == red)) status = flu;
if ((numeratorDF == even) || (denominatorDF == even)) formula = 26;
if (((clock == 9.) && (sun != shining))&& (location == US)) time = pm;
Order of Operations
We are very familiar with the order of arithmetic operations because of our experience with arithmetic and elementary algebra. However, the logical operators are less familiar to many. There is, as might be imagined, a specific order for evaluating expressions in C++. To avoid confusion in using these operators study the following table; the arithmetic, logical, and assignment operators are listed in the order of evaluation.
| Description | Operators |
|---|---|
| Grouping symbols | ( ) |
| Unary operators | ! (not) +/- (as in +2 or -9.3) |
| Multiplication, Division, Modulus | * / % |
| Addition and Subtraction | + - |
| Arithmetic comparison | < <= > >= |
| Logical equal and not equal | == != |
| Logical AND | && |
| Logical OR | || |
| Assignment statement | = |
EXERCISES
1. An
example
program for assignment statements was given. Enter the program and compile and
execute it, verifying that the comments are correct. After
examining your output, answer two questions:
(a) i was assigned the value 1 - why does the output say its value is
3?
(b) What happened to the value of surprise?
2. For the arithmetic expressions below, assume that all numbers without decimal points are integers, and numbers with decimal points are floats. Fill in the following table by, first, writing down what you think the computer will get for a result. Then write, compile, and run a program to print out the results of the calculations. (Remember the order of operations and also be careful with integer division.)
If you wish, you may choose to write one program with all the calculations, or you may write more than one program if you need to practice the process of compiling. We have not yet discussed how to tell the computer to print in C++, so a skeleton program is provided for you.
// Program skeleton A for problem output.
#include <iostream.h> // For the output statements
main ()
{
// Sample output statement to print the calculation results
//of one line.
cout << "3 + 5 - 2 = " << 3 + 5 - 2 << endl;
return (0);
}
| Expression |
Your prediction of result |
Computer’s result |
|
| a) | 3 + 5 - 2 | ||
| b) | 1.2 +2.1 *2.1 | ||
| c) | (1.2 +2.1) * 2.1 | ||
| d) | (1.5 + 1.5)/1.5 - 0.75 | ||
| e) | 64.0 / 4.0 /8.0 | ||
| f) | (3 + 5) % (5 - 3) | ||
| g) | 3/8 + 5/8 |
3. Remember, in C++ logical expressions have a nonzero value if the expression is true, and a zero value if the expression is false. Assuming that
a = 1, b = 2, and c = 3
fill in the following table by, first, writing down what you think the computer will get for a result. Your response should be NOT ZERO for true and ZERO for false. Then write, compile, and run a program to print out the results of the calculations.
As before, one program will be sufficient; a skeleton program is provided. For the program to compile without error, note that parentheses were added around the value, (a==b), in the output statement.
// Program skeleton B for problem output.
#include <iostream.h> // For the output statements
//Data declarations
int a, b, c;
main()
{
a = 1;
b = 2;
c = 3;
// Sample output statement to print the calculation results
//of one line.
cout << "a==b " << (a==b) << endl;
return (0);
}
|
Logical Expressions |
Your prediction of result |
Computer’s result |
|
| a) | a==b | ||
| b) | (a + 1) < b | ||
| c) | (a < b) || (b < c) | ||
| d) | (a != b) && (b != c) | ||
| e) | (7 < c) || (a == c-2) | ||
| f) | (5 > c) && (b <= a) |
4. The following expressions are more difficult to evaluate because there are no parentheses. In order to evaluate the expressions the order of operations rule must be used. Assuming that
x = 1.2, y = 6.0, and z = 2.4,
rewrite the expression with parentheses (the first two are done for you); then specify whether the result is NOT ZERO for true or ZERO for false. Be careful to distinguish between = and == in the problems below.
|
Expression |
Expression with parentheses |
Result |
|
| a) | x > z && y > z | ((x > z )&& (y > z)) | |
| b) | x <= 5 || y > 2 || z == 6.0 | (((x <= 5) || (y > 2)) || (z == 6.0)) | |
| c) | x == 1.3 | ||
| d) | x == 1.3 || y > 5.0 && z > 2.0 | ||
| e) | x <= y && y <= z || x > y && y > z | ||
| f) | x + y >= z && z == 10.0 | ||
| g) | z == 2.4 || x + y > z |