810201
Computer Skills 2
Chapter 4 - C++ Operators
Professor Nasser Abdellatif
Summer 2020
Chapter 4 C++ Operators
An operator is a symbol that tells the compiler to perform specific mathematical
or logical functions. C language is rich in built-in operators and provides the
following types of operators −
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
We will, in this chapter, look into the way each operator works.
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then −
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand A − B = -10
from the first.
* Multiplies both operands. A * B = 200
/ Divides numerator by de- B/A=2
numerator.
% Modulus Operator and B%A=0
remainder of after an
integer division.
Addition
Examples:
int x=2, y=5; int z=x+y; (value of z = 7)
float x=2.5, y=3.2; float z=x+y; (value of z=5.7)
int x=2; int y=x+2.5; (value of y =4 (no decimal))
Subtraction
int x=5, y=10; int z=x+y-30; (value of z=-15)
Multiplication:
int x=2, y=4; int z=x*y;
int y=x+y*5;
value of z=8
value of y=22 (multiplication is done first 4*5=20+2=22)
Division: Memory
float x=10/4; (value of 10/4=2) x= 2.0
float x=10.0/4.0; (value of 10.0/4.0=2.5) x= 2.5
float x=10/4.0; (Value pf 10/4.0=2.5) x= 2.5
int x=10/4; (value of 10/4=2) x= 2
int x=10.0/4; (value of 10.0/4=2.5) x= 2
int x=0.5+1/2; (value of, 0.5+0=0.5) x= 0
int x=0.5+1.0/2; (value of 0.5+0.5=1.0) x= 1
Modulus %
int x=34%5; (value of x=4) remainder
34/5 =6 and reminder 4
int x=-34%5; (Value of x=-4) sing of numerator
int x=34%-5; (value of x=4) sing of numerator
int x=-34%-5; (value of x=-4) sign of numerator
int x=4%6; (value of x=4, 4/6=0 remainder 4)
Anytime the first is less than the second the value equals the first
int x=7%9013; (Value of x=7)
The result always follows the sign of the first.
Results:
int/int=int int/float=float float/int=float float/float=float
int x=5+2 *(3/2 + 7%2 +1/2 -1%2 +4.5);
x=5+2 *(1 + 1 + 0 -1 +4.5)
x=5 +2 * (7.5) =5 +15 = 20
int x=2, y=5, z=7;
int w=x+2*y+z/2.0;
w=x + 10 + 3.5 = 15.5 However w is integer and therefore w=15
Note: Always check where the will the result is to be stored.
Precedence
1. ( )
2. * / % (from left to right)
3. + - ( from left to right)
Examples:
7%5*2=2*2=4 2+5*2+5%7 +(1+2)*4
2+5*2 +5%7 +3*4 2+10 + 5 +12= 29
Example: Write a program that reads two integer numbers then find the sum.
#include <iostream> Output screen
using namespace std; Enter x and y
int main ( ) { 7 (enter key)
int x, y; 2 (enter key)
cout << “Enter x and y \n”; 9
cin >> x>>y;
int z=x+y;
cout <<z; }
Example 2: Write a program that read the radius of a circle then find the area of
the circle (area=pi* r^2).
#include <iostream> Output screen
using namespace std; enter the value of
int main ( ) { the radius
float r; 2.0 (enter key)
cout <<"enter the value of the radius \n"; area = 12.56
cin >>r;
float area=3.14*r*r;
cout <<"area = "<<area;
}
Example 3: Write a program that reads the weight in kilograms then finds the
weight in pounds. 1 pound =0.45 kilogram.
#include <iostream>
using namespace std;
int main ( ) {
float wk;
cout << “enter the weight in kilograms \n”;
cin >>wk;
float wp=wk/0.45;
cout << “weight in pounds = “<<wp;
}
Exercise 1: Write a program that reads the distance and the time, then find the
speed. Speed=distance/time.
Exercise 2: Write a program that reads the time in hours, minutes and seconds,
then find the total time in seconds.
Shortcut Operators
Name Operation Example Equivalent Memory
Addition += int x-5, y=3; int x=5, y=3; x=5 y=3
x +=y; x=x+y; x=8
Subtraction -= int x=5; int x=5; x=5
x -=3; x=x-3 x=2
Multiplication *= int x=5, y=3; int x=5, y=3; x=5 y=3
x*=y+3 x=x*(y+3) x=5*6=30
Division /= int x=5, y=2; int x=5, y=2; x=5 y=2
x/=y; x=x/y; x=2
Modulus %= int x=-5, y=-7; int x=-5, y=-7; x=-5 y=-7
x %=y; x=x%y; x=-5
Example: int x=2, y=3;
x +=y x=2 y=3 x=5 y +=x x=2 y=3 y=5
Increment /Decrement
Increments: are two types
Post increment x++ x=x+1
Pre increment ++x x=x+1
Example 1:
int x=5; x=5
int y,w;
x++; x=6
y=++x; (pre-increment x=x+1 then y=x) x=7, y=7
w=2*x++;(Post-increment w=2*x then x=x+1) w=2*7=14, x=8
Example 2:
int x=5; x=5
int y, w;
++x; x=6
y=++x; y=7 x=7
w=2*(++x); w=2*8=16 x=8
If we have x++ or ++x alone, we add 1 to the value of x in the memory.
If we have a post increment (x++) in a statement, First the statement is executed
using the current value of x (old value) and second increment the value of x (we
add 1 to the value of x in the memory). That is, we execute the statement with
the current value of x then we increment x by adding 1.
If we have a pre increment in a statement, we increment x by 1 first, then we
execute the statement using the new value of x.
Decrement: there are two types as well
Post decrement x--; x=x-1;
Pre decrement –x; x=x-1;
Decrement has the same rules as increment.
int x=5, y=3; x=x+y--; x=5 y=3
x+= y--; x=5+3=5+3=8 y=2
int x=5, y=3; x=x+(--y); x=5 y=3
x+= --y; x=x+(y-1); x=5+(3-1)=7 y=2
Example 1:
Program Memory Output
#include <iostream> w x y 6
using namespace std; 7 5 10 8
int main ( ) { 6 6 11 9
int x=5, y=10, w=7; 11 7 10 11
y++; 8 9 9
--w; 8
w +=x++; 12
cout << x++<< endl; 11
cout <<++x << endl;
cout << x+1 << endl;
cout <<y-- << endl;
cout << (y--)-1<< endl;
cout << y-1 << endl;
cout << w+1 << endl;
cout << w;
}
Relational Operators
Name Operatio Example Answer
n
Greater than > 1>2 False
Greater than or equal >= 1>=2 False
Less than < 1<2 True
Less than or equal <= 1<=2 True
Equal to == 1==2 False
Not equal != 1!=2 True
Relational Operators
• Note: ASCI code
‘A’=65, ‘B’=66, ‘C’=67, …., ‘Z’=90
‘a’=97, ‘b’=98, … , ‘z’=122
‘m’ > ‘M’ (T)
‘a’ < ‘Z’ (F)
• Note: Capital letters have smaller values than small letters.
• Example:
char a=’R’, b=’D’;
a<b (F) variable ‘a’<’b’ (T) value
Logical Operators
Name Operation
AND &&
OR ||
NOT !
Logical Operators
AND truth table
c2 c1 c1&&c2
F F F
F T F
T F F
T T T
Logical Operators
OR truth table
c2 c1 c1||c2
F F F
F T T
T F T
T T T
Logical Operators
NOT truth table
c1 !c1
T F
F T
Logical Operators
Examples: Find T/F
1. (5>7 || 2==1) && !(!(‘b’>’a’)&&(‘A’>’d’))
(F ||F) && !(!(T) && (F))
F && !(F && F) = F && !(F) = F && T = F
Once we knew that the first if False there was no need to continue since F && ? =
F
2. (‘a’ < ‘b’ ) || (x > B )
T ||? = T
3. !(! (! (5==2) && !(11==1)))
!(!(!(F) && !(F))) =!(!(T) && T))=!(!(T && T)) = !(!(T)) = !(F) = T