You are on page 1of 23

4 Operators and Expressions

4.1 Operators Learning Objectives


4.2 Arithmetic Operators
4.2.1 Unary Operator Upon successful completion of this chapter, students will be
4.2.2 Binary Operators able to:
4.2.3 Increment and Decrement
Operator § Describe operators
4.2.4 Precedence of arithmetic
Operators § Demonstrate an understanding of arithmetic operators,
relational operators, and logical operator
4.3 Relational Operators § Use assignment operator
§ Demonstrate an understanding of operator precedence
4.4 Logical Operators
§ Describe the various forms of expression
4.5 Assignment Operator

4.6 Precedence of Operators

4.7 Expressions
4.7.1 Mixed Mode Expressions and
Cast
4.7.2 Spacing and Parentheses

4.8 Sample Programs


4.8.1 Sample Program 1
4.8.2 Sample Program 2
4.8.3 Sample Program 3

4.9 Summary

4.10 Exercises
Fundamentals to Programming

In this chapter, we will discuss the operators and expressions, and the assignment statement
in C++ programming language.

4.1 Operators

A mathematician uses more than just the variables. A mathematician can add them together,
subtract them, multiply them, and perform an almost endless list of other operations.

C++ offers the same set of basic operations. C++ programs can multiply, add, divide, and so
forth. Programs have to be able to perform these operations in order to get anything done.

C++ is very rich in built-in operators. Operator triggers some computations when applied to

4
operands in an expression. The commonly used operators are as follows:

§ Arithmetic operators
§ Relational operators
§ Logical operator
§ Assignment operator

Table 4-1 shows the commonly use operators in C++ programming language.

Arithmetic Relational Logical operator Assignment


operators operators operator
+ > && =
- < ||
* >= !
/ <=
% ==
Table 4-1 Commonly used operators in C++

4.2 Arithmetic Operators

Expressions are made up of constants, variables, and operator. The following are all valid
arithmetic expressions:

beta + 4
rate – 1.5
2- price
price * discount
m * x + c

The operators allowed in an expression depend on the data types of the constants and
variables. Table 4-2 shows the arithmetics operators used in C++ programming language.

72
Operators and Expressions

Arithmetic Operator Description


+ Unary plus
- Unary minus
+ Addition
- Substraction
* Multiplication
/ Floating-point division (floating-point result)
Intreger division (no fraction part)
% Modulus (remainder from the integer division)
Table 4-2 Arithmetics operators

The first two operators are unary operator. Unary oprarators just take one operand
(argument). The remaining five are binary operators. Binary operators take two operands.

4.2.1 Unary Operator


4
The + and - operator can be unary or binary. A unary operator has only one operand (in
other words, unary operators take a single argument) while a binary operator has two
operands. For example, the - operator in -5 can be considered as a unary operator to negate
number 5, whereas the - operator in 4 -5 is a binary operator for subtracting 5 from 4.

You an assign a positive or negative number to a variable by using a unary + or -. For


instance,

m = +12 // Assigns 'm' a positive 12


n = -12 // Assigns 'n' a negative 12

Unary operators operate on a single value. Thus sizeof operator, decrement operator and
increment operator are unary operators.

Here is a program example using unary operator.

// Program that uses unary operator


#include <iostream.h>

main()
{
int a = +10;
int b = -a;
int c = -b;
int d = -(-b);

cout<<"a = "<<a<<endl;
cout<<"b = -a = "<<b<<endl;
cout<<"c = -b = "<<c<<endl;
cout<<"d = -(-b) = "<<d<<endl;
}
Program 4-1 Program that uses unary operator

73
Fundamentals to Programming

Here is the output.

a = 10
b = -a = -10
c = -b = 10
d = -(-b) = -10

4.2.2 Binary Operators

A binary operator is one that has two arguments. If you can say var1 op var2, op must
be a binary operator. The most common binary operators are the simple operations you
performed in grade school.

4 There are five standard arithmetic operators in C++ as shown in following table. The
arithmetic operators are use to manipulate values in programs.

Operator Description Example


+ Addition 25 + 2, the result is 27
- Subtraction 25 - 2, the result is 23
* Multiplication 25 * 2, the result is 50
/ Division 25 / 2, the result is 11 for integer; 11.5 for floating point
% Modulus 25 % 2, the result is 1 (that is, 25 / 2 = 11 with
(remainder) remainder of 1)
Table 4-3 Binary arithmetic operators

When you divide two integers, the result will be an integer. In other words, any fractional
part of the result is lost. For example, the result of 25 / 2 is 11, even though is 11.5 in a
mathematical expression.

When you use modulus operator with two integers, the result is an integer with the value of
the remainder after division takes place, so the result of 25 % 2 is 1.

Here are some examples of operations involving integer variables. The expressions on the left
evaluate to the values on the right. Let say

m = 5
n = 2

Expression Result
m + n 7
m - n 3
m * n 10
m / n 2 ← Remainder is discarded
m % n 1 ← Produces remainder of division
Table 4-4 Arithmetic operation involving integer variable

When the forward slash / is applied to integer operands, any remainder with be truncated
while the modulus (%) produces the remainder of the division.

74
Operators and Expressions

The modulus operator (%), increment operator (++) and decrement operator (--) only can be
used with integers. You can not use with floating-point data.

Here is an example using arithmetic operators.

// Program using arithmetic operators


# include <iostream.h>

main()
{
int value1 = 43, value2 = 10, sum;
int difference, product, quotient, modulus;

sum = value1 + value2;


difference = value1 - value2;

4
product = value1 * value2;
quotient = value1 / value2;
modulus = value1 % value2;

cout<<"\nSum is "<<sum<<endl;
cout<<"Difference is "<<difference<<endl;
cout<<"Product is "<<product<<endl;
cout<<"Quotient is "<<quotient<<endl;
cout<<"Modulus is "<<modulus<<endl;
return 0;
}
Program 4-2 Program using arithmetic operators

Here is the output.

Sum is 53
Difference is 33
Product is 430
Quotient is 4
Modulus is 3

4.2.3 Increment and Decrement Operator

In C++, the increment operator is ++ and means “increase by one unit.” The decrement
operator is -- and means “decrease by one unit.”

If x is an int, for example, the expression ++x is equivalent to (x = x + 1). Increment


and decrement operators produce the value of the variable as a result.

There are two versions of each type of operator, often called the prefix and postfix versions
which is summarized in the table 5-3. Pre-increment means the ++ operator appears before
the variable or expression, and post-increment means the ++ operator appears after the
variable or expression.

Similarly, pre-decrement means the -- operator appears before the variable or expression,
and post-decrement means the -- operator appears after the variable or expression.

75
Fundamentals to Programming

Operator Called Expression Explanation m = 5, n = 2


++ Pre-increment ++m Increment m by 1, then the 6
new value of m in the
expression in which m
resides.
++ Post-increment m++ Use the current value of m in 6
the expression in which m
resides, then increment m by
1.
-- Pre-decrement --n Decrement n by 1, then use 1
the new value of n in the
expression in which n

4
resides.
-- Post-decrement n-- Use the current value of n in 1
the expression in which n
resides, then decrement n by
1.
Table 4-5 The increment and decrement operators

For pre-increment and pre-decrement, (i.e., ++a or --a), the operation is performed and the
value is produced. For post-increment and post-decrement (i.e. a++ or a--), the value is
produced, then the operation is performed.

Here is an example using pre-increment and post-increment operator.

// Program to show pre-incrementing and post-incrementing operation


#include <iostream.h>

main()
{
int n; // Declare varible n
n = 5; // Set n equal to 5
cout<<n<<endl; // Display n
cout<<++n<<endl; // Add 1 to n then Display n
cout<<n<<endl; // Display n

cout<<endl; // Skip a line

n = 5; // Set n equal to 5 again

cout<<n<<endl; // Display n
cout<<n++<<endl; // Display n then add 1 to n
cout<<n<<endl; // Diaplay n
return 0;
}
Program 4-3 Program to show pre-incrementing and post-incrementing operation

76
Operators and Expressions

Here is the output.

5
6
6

5
5
6

4.2.4 Precedence of arithmetic Operators

Arithmetic operators have the following precedence:

Highest Arithmetic Operator

Unary Operators
Operator

+ - ++ --
4
Arithmetic multiply, divide, remainder (modulus) * / %
Arithmetic add and subtract + -

Lowest
Figure 4-1 Precedence of arithmetic operators

When you combine mathematical operations in a single statement, you must understand
operator precedence, or the order in which parts of a mathematical expression are evaluated.
Multiplication, division and modulus always take place prior to addition and subtraction in an
expression. For the expression

int result = 2 + 3 * 4;

The results is 14, because the multiplication (3 * 4) occurs before adding 2. You can
override normal operator precedence by putting the operation to perform in parentheses. The
expression

int result = (2 + 3) * 4;

results in 20, because the addition within the parentheses takes place first, and then that result
(5) is multiplied by 4.

77
Fundamentals to Programming

4.3 Relational Operators

Relational operators are also known as comparison operators. A relational operator compares
two items. There are six relational operators in C++ as shown in the table below.

Operator Description True Example False Example


< Less Than 3 < 8 8 < 3
> Greater Than 4 > 2 2 > 4
== Equal to 7 == 7 3 == 9
<= Less than or equal to 5 <= 5 8 <= 6
>= Greater than on equal to 7 >= 3 1 >=2
!= Not equal to 5 != 6 3 != 3

4
Table 4-6 Relational operators

The results of evaluation of a relational operation is either true (represented by 1) or false


(represented by 0).

When you use any of the operators that have two symbols (==, <=, >=, or !=), you cannot
place any whitespace (blank space) between the two symbols.

78
Operators and Expressions

Here is a sample program testing therelational operators.

// Program that testing the relational operators


#include <iostream.h>

main()
{
cout<<"The results of evaluation of a relational operation"<<endl;
cout<<"true (represented by 1) or false (represented by 0)."<<endl;
cout<<endl; // An empty line

// Testing greater than


cout<<"Testing greater than"<<endl;
cout<<"100 > 10 results in "<<(100 > 10)<<endl;
cout<<"10 > 100 results in "<<(10 > 100)<<endl;
cout<<"100 > 100 results in "<<(100 > 100)<<endl;

// Testing less than


cout<<"\nTesting less than"<<endl;
cout<<"10 < 100 results in "<<(10 < 100)<<endl;
4
cout<<"100 < 10 results in "<<(100 < 10)<<endl;
cout<<"100 < 100 results in "<<(100 < 100)<<endl;

// Testing greater than or equal to


cout<<"\nTesting greater than or equal to"<<endl;
cout<<"100 >= 10 results in "<<(100 > 10)<<endl;
cout<<"10 >= 100 results in "<<(10 > 100)<<endl;
cout<<"100 >= 100 results in "<<(100 >= 100)<<endl;

// Testing less than or equal to


cout<<"\nTesting less than or equal to"<<endl;
cout<<"10 <= 100 results in "<<(10 < 100)<<endl;
cout<<"100 <= 10 results in "<<(100 < 10)<<endl;
cout<<"100 <= 100 results in "<<(100 <= 100)<<endl;

// Testing equal to
cout<<"\nTesting equal to"<<endl;
cout<<"100 == 100 results in "<<(100 == 100)<<endl;
cout<<"100 == 10 results in "<<(100 == 10)<<endl;
cout<<"10 == 100 results in "<<(10 == 100)<<endl;

return 0;
}
Program 4-4 Program that testing the relational operators

79
Fundamentals to Programming

Here is the output.

The results of evaluation of a relational operation


true (represented by 1) or false (represented by 0).

Testing greater than


100 > 10 results in 1
10 > 100 results in 0
100 > 100 results in 0

Testing less than


10 < 100 results in 1
100 < 10 results in 0
100 < 100 results in 0

Testing greater than or equal to

4
100 >= 10 results in 1
10 >= 100 results in 0
100 >= 100 results in 1

Testing less than or equal to


10 <= 100 results in 1
100 <= 10 results in 0
100 <= 100 results in 1

Testing equal to
100 == 100 results in 1
100 == 10 results in 0
10 == 100 results in 0

4.4 Logical Operators

In addition to use simple relational expressions, we can create more complex expressions by
using the logical operations AND, OR and NOT.

The three logical operators in C++ are represented by the symbols as shown in the Table 4-7
below.

Operator Description
&& AND
|| OR
! NOT
Table 4-7 Logical operators

The results of logical operations on m an n are summarized in Table 4-8 below.

M n m && n m || n !m
Both m and n must be Either m or n must be Produces the opposite
true true relation
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Table 4-8 Logical operations on m and n

80
Operators and Expressions

Here is a sample program testing the logical operators.

// Program that testing the logical operators


#include <iostream.h>

main()
{
// Output to screen
cout<<"true = 1, false = 0"<<endl;

// Testing the logical operator AND, &&


cout<<"\nfalse AND false results in "<<(0 && 0)<<endl;
cout<<"false AND true results in "<<(0 && 1)<<endl;
cout<<"true AND false results in "<<(1 && 0)<<endl;
cout<<"true AND true results in "<<(1 && 1)<<endl;

// Testing the logical operator OR, ||


cout<<"\nfalse OR false results in "<<(0 || 0)<<endl;
cout<<"false OR true results in "<<(0 || 1)<<endl;
4
cout<<"true OR false results in "<<(1 || 0)<<endl;
cout<<"true OR true results in "<<(1 || 1)<<endl;

// Testing the logical operator NOT, !


cout<<"\nNOT false results in "<<(!0)<<endl;
cout<<"NOT true results in "<<(!1)<<endl;

return 0;
}
Program 4-5 Program that testing the logical operators

Here is the output.

true = 1, false = 0

false AND false results in 0


false AND true results in 0
true AND false results in 0
true AND true results in 1

false OR false results in 0


false OR true results in 1
true OR false results in 1
true OR true results in 1

NOT false results in 1


NOT true results in 0

81
Fundamentals to Programming

4.5 Assignment Operator

C++ has several assignment operators. The most commonly used assignment operator is =.
The assignment operator is used to change the value of a variable. Assignment operations
using = has the general form

identifier = expression;

where identifier generally represents a variable and expression represent a value, constant, a
variable or a more complex expression. Here are some examples of assignment operations.

num_of_seat = 50;
interest_rate = 0.05;

4 char newline = '\n';


area_of_rectangle = (width + height) * 2;
salary = basic_pay + overtime_hours * overtime_rate;

There is a shorthand notation that combines the assignment operator (=) and an arithmetic
operator so that a given variable can have its value changed by adding, subtracting,
multiplying by, or dividing by a specified values. The general form is

identifier = arithmetic_operator (expression);

The expression can be another variable, a constant or more complicated arithmetic expression.
Below are examples:

Assignment Statement Equivalent to


count += 2; count = count + 2;
total -= discount; total = total - discount
bonus *= 2; bonus = bonus * 2;
time /= rush_factor; time = time / rush_factor
change %= 100; change = change % 100;
amount *= count1 + count2; amount = amount * (count1 + count 2)

82
Operators and Expressions

// Program that demonstrating the assignment operators


#include <iostream.h>

main()
{
// Variable declaration and initialization
float m = 0.0, n = 0.0, result = 0.0;

// Prompt for input


cout<<"Enter 2 numbers: ";
cin>>m>>n;

// Testing the assignment (=) operator


result = m + n;
cout<<m<<" + "<<n<<" = "<<result<<endl;

// Testing the assignment (=) operator


result = m;
result = result - n;
4
cout<<m<<" - "<<n<<" = "<<result<<endl;

// Testing the assignment (=) operator


result = n;
result = result * m;
cout<<m<<" * "<<n<<" = "<<result<<endl;

// Testing the assignment (=) operator


result = m;
result /= n;
cout<<m<<" / "<<n<<" = "<<result<<endl;

return 0;
}
Program 4-6 Program that testing the assignment operators

Here is the result of sample run.

Enter 2 numbers: 5 2
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2.5

83
Fundamentals to Programming

4.6 Precedence of Operators

The hierarchy of operator precedence from highest to lowest is summarized in figure below.

Highest Operator Category Operator


Unary Operators ++ -- + -
Arithmetic multiply, divide, remainder * / %
Arithmetic add and subtract + -
Relational operators > < >= <= == !=
Logical operators && || !
Assignment =
Lowest

4
Figure 4-2 Precedence of operators

Here is a sample program testing the operator precedence from highest to lowest.

// Program that testing the precedence of operators


#include <iostream.h>

main()
{
int result = 0;
int a = 2, b = 3;
int m = 2, n = 3;
int p = 2, q = 3;
int x = 2, y = 3;

result = ++a + --b * 4 > ++m * --n + 4


&& p++ + q-- * 4 < x++ * y-- + 4;

cout<<"\nResult = "<<result;

return 0;
}
Program 4-7 Program that testing the precedence of operators

Here is the output.

Result = 0

The precedence of operators in Program 4-7 is illustrated in Figure 4-3.

Figure 4-3 Example illustrates the precedence of operators

84
Operators and Expressions

4.7 Expressions

Expressions in C++ are formed by properly combining operators, variables and constants. As
in algebra, C++ expression can be complex. Parentheses are used to force the order of
evaluation. An expression may also contain spaces for readability. Here are some examples
of C++ expressions.
gross_pay – deductions
(basic_pay + hours * rate) – (socso +epf)
(k * k - 5 * j* h) >100
(gender == 'm') && (age > 20)
(size == 'M' || size = 'L') && price <= 20

4.7.1 Mixed Mode Expressions and Cast


4
When variables and constants of different data types are mixed in an expression, they are
converted to the “biggest” or “longest” data type, in other words, data type that requires the
most number of byte, before it is evaluated.

To illustrate this, lets look at the following program

// Program to illustrate data conversion


#include <iostream.h>

main()
{
int a = 5, b = 2; // both a and b are type int
float c = 3.0, d;
d = c + a / b;
cout<<"Result = "<<d;
return 0;
}
Program 4-8 Program to illustrate data conversion

Here is the output.

Result = 5

The expression a / b is evaluated to 2 since both a and b are of type int and no data
convertion takes place (the fraction part .5 is dropped. Next, since c is of type float
(require more bytes), the result is then added to the value in c (3.0). The end result 5.0 is
finally stored in d.

However if a or b is declared as type float, then a / b would evaluate to 2.5 in which case d
would have the value 5.5 as shown in following program.

85
Fundamentals to Programming

// Program to illustrate data conversion


#include <iostream.h>

main()
{
int a = 5;
float b = 2, c = 3.0, d; // c and d is now declare as float
d = c + a / b;
cout<<"Result = "<<d;
return 0;
}
Program 4-9 Program to illustrate data conversion

Here is the output.

4 Result = 5.5

The function cast may be used to force an expression to be of a specific data type. It has the
general forms

(type)expression
expression(type)

With reference to the statement d = c + a / b;, any of the following six expressions can be
used to preserve the fractional part of a / b.

c + (float)a / b;
c + float (a) / b;
c + a / (float)b;
c + a / float (b);
c + (float)a / (float)b;
c + float(a) / float(b);

4.7.2 Spacing and Parentheses

You may place space in an expression or a statement to make it more readable. Use of or
additional or redundant parentheses will not cause errors or slow down the execution of an
expression. It is shows in Program 4-10 below.

// Program to test the spacing and parentheses


#include <iostream.h>

main()
{
{
{
{
cout<<"Result = ";
cout<<((((1 + 2) + 3) + 4) + 5);
}
}
}
}
Program 4-10 Program to test the spacing and parentheses

86
Operators and Expressions

Here is the output.

Result = 15

87
Fundamentals to Programming

4.8 Sample Programs

4.8.1 Sample Program 1

Program 4-11 begins with a comment that explains what the program does. The body of the
main function includes a declaration section where the variables w, h, perimeter and
area are defined and initialized. It is follow by a sequence of executable statements. These
statements compute the perimeter and area. Finaly, it display results to screen.

// Program to compute perimeter and area

4
#include <iostream.h>

main()
{
// Variable declaration and initialization
float w = 0.0, h = 0.0, perimeter = 0.0, area = 0.0;

// Prompts for input


cout<<"Enter the rectangle width: ";
cin>>w;
cout<<"Enter the rectangle height: ";
cin>>h;

// Computes the perimeter


perimeter = 2 * (w + h);

// Computes the area


area = w * h;

// Output results to screen


cout<<"Perimeter = "<<perimeter<<endl;
cout<<"Area = "<<area<<endl;

return 0;

}
Program 4-11 Program to compute perimeter and area

Here is the output of sample run.

Enter the rectangle width: 4.0


Enter the rectangle height: 8.00
Perimeter = 24
Area = 32

88
Operators and Expressions

4.8.2 Sample Program 2

Program 3-10 begins with a comment that explains what the program does. The body of the
main function includes a declaration section where the variables value1 and value2, and
are defined. It is follow by a sequence of executable statements. These statement display the
comparison results of variables value1 and value2.

// Program that comparing two numbers entered from keyboard


#include <iostream.h>

main()
{
// Variable declaration and initialization

4
float value1 = 0.0, value2 = 0.0;

// Input and output statements


cout<<"Enter first number: ";
cin>>value1;
cout<<"Enter second number: ";
cin>>value2;

// Output result to screen


cout<<"\nTrue = 1, false = 0"<<endl;
cout<<value1<<" > "<<value2<<" results in "<<(value1 > value2)<<endl;
cout<<value1<<" < "<<value2<<" results in "<<(value1 < value2)<<endl;
cout<<value1<<" >= "<<value2<<" results in "
<<(value1 >= value2)<<endl;
cout<<value1<<" <= "<<value2<<" results in "
<<(value1 <= value2)<<endl;
cout<<value1<<" == "<<value2<<" results in "
<<(value1 == value2)<<endl;

return 0;
}
Program 4-12 Program that comparing two numbers entered from keyboard

Here is the result of sample run.

Enter first number: 2.3


Enter second number: 9.8

True = 1, false = 0
2.3 > 9.8 results in 0
2.3 < 9.8 results in 1
2.3 >= 9.8 results in 0
2.3 <= 9.8 results in 1
2.3 == 9.8 results in 0

89
Fundamentals to Programming

4.8.3 Sample Program 3

Program 4-13 computes the amount earned. The program starts with the declaration and
initialization of constant variable rate, and variable total and day. Given the number of
days worked and the daily rate, the program first converts or casts the value of day (an
integer) to a floating-point number before using it in the multiplication. Finaly, it displays the
result to screen.

// Program to calculate the amount of money earned


#include <iostream.h>

main()
{

4
// Constant & variable declaration and initialization
const float rate = 50.00;
float total = 0.00;
int day = 0;

// Prompts for input


cout<<"Enter entered number of days worked: ";
cin>>day;

// Computes the perimeter


total = (float) day * rate; // Converts day to floating point

// Output results to screen


cout<<"\nFor "<<day<<" days of work with the daily rate $ "<<rate;
cout<<", you have earned $ "<<total<<".";

return 0;
}
Program 4-13 Program to calculate the amount of money earned

Here is the output of sample run.

Enter entered number of days worked: 10

For 10 days of work with the daily rate $ 50, you have earned $ 500.

90
Operators and Expressions

4.9 Summary

§ The commonly used operators in C++ are arithmetic operators, relational operators,
logical operators, and assignment operators.

§ There are five standard arithmetic operators: +, -, *, /, %.

§ There are six comparison operators: >, <, >=, <=, ==, !=.

§ There are three logical operators: &&, ||, !.

§ The assignment operator, = is used to change the value of a variable.

§ The hierarchy of operator precedence from highest to lowest is:


Unary Operators
4
Arithmetic multiply, divide, remainder
Arithmetic add and subtract
Relational operators
Logical operators

§ Expressions in C++ are formed by properly combining operators, variables and


constants.

§ White spaces and additional parentheses are placed in an expression or a statement to


make it more readable.

91
Fundamentals to Programming

4.10 Exercises

1. What is the numerical value of each of the following expressions as evaluated by the
C++ programming language?

a. 3+6*4
b. 6/3*8
c. 18 / 2 + 14 /2
d. 16 % 2
e. 17 % 2
f. 28 % 5
g. 28 % 5 * 3 + 1

4
h. 20 / (4 +1)
i. (10 + 20) / 5
j. 'B' - 'A'

2. What is the value of each of the following expressions?

a. 4>1
b. 6<= 18
c. 42 >= 42
d. 6 == 9
e. 2 + 5 == 7
f. 8 + 3 <= 10
g. 3 != 9
h. 13 != 13
i. -4 != 4
j. 2 + 5 * 3 == 21

3. Given integer variables x, y, and z with values 10, 7, and 2 respectively. Determine
the valued of each of the following arithmetic expressions.

a. x + y - 2z
b. x / z (x * x +y)
c. (x * y) % z
d. 5 * (x + y + z) – x / z
e. x*y–x*z
f. y * (x + z) * (x – y)

4. Using the values given in exercise 3, determine the values for the each af the
following expressions:

a. x++
b. x < 2 && z > 5.0
c. y >= x
d. z != 10
e. x < y || y > z
f. (z * 5) == x

92
Operators and Expressions

5. The assignment operator in the C++ programming language is ____________.

a. =
b. ==
c. =>
d. +=

6. The “equal to” comparison operator is ____________.

a. =
b. ==
c. !=
d. !!

7. If you attempt to add a float, an int and a long, the result will be a(n) ____________.

a. float
4
b. int
c. long
d. error message

8. Which assignment is correct?

a. char code = 3;
b. char code = "three";
c. char code = "3";
d. char code = '3';

9. What is the output of the following program lines?

a = 'b';
b = 'c';
c = a;
cout<<a<<b<<c;

10. Write a complete C++ program that reads two whole numbers into two variables of
type int, and then output both the whole number part and the remainder when first
number is divided by the second. This can be done using the operators / and %.

93