Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Operators Topics

Are a set of keywords and signs that are not part


of the alphabet but are available in all
keyboards.
• Assignment
• Arithmetic operators
• Relational operators
• Logic operators
• Order of Precedence

(C) Copyright 2020 by Prof.Abeer El_korany


1
1- Assignment Statement
• The assignation operator serves to assign a value to a variable.
• Basic form
– variable = expression ;
– i = 5; ch = `y';
• Action
– Expression is evaluated
– Expression value stored in lvalue
– The assignation operation always takes place from right to
left and never at the inverse.
int a, b; // a:? b:?
a = 10; // a:10 b:?
b = 4; // a:10 b:4
a = b; // a:4 b:4
b = 7; // a:4 b:7 Does this affect the value of a
Is this valid operation a = b = c = 5;
(C) Copyright 2020 by Prof.Abeer El_korany
2
Assignment Statement
int NewStudents = 6; NewStudents 6
int OldStudents = 21; OldStudents 21
int TotalStudents; TotalStudents ?

TotalStudents = NewStudents + OldStudents;

NewStudents 6
OldStudents 21
TotalStudents 27

(C) Copyright 2020 by Prof.Abeer El_korany


3
Assignment Statement (cont.)
int Value1 = 10; Value1 10
20

int Value2 = 20; Value2 20

int Hold = Value1; Hold 10

Value1 = Value2; Value1 20


Value2 10
Value2 = Hold; Hold 10

In order to swap the values of Value1 and Value2


using Hold as temporary holder

(C) Copyright 2020 by Prof.Abeer El_korany


4
Other Similar Statements

(C) Copyright 2020 by Prof.Abeer El_korany


5
Combined Assignment
• The combined assignment operators provide a
shorthand for these types of statements.
• The statement
sum = sum + 1;
is equivalent to
sum += 1;

(C) Copyright 2020 by Prof.Abeer El_korany


6
Combined Assignment Operators

(C) Copyright 2020 by Prof.Abeer El_korany


7
Arithmetic Operators
• Used for performing numeric calculations
• C++ has unary, binary, and ternary operators:
– unary (1 operand) -5
– binary (2 operands) 13 - 7
– ternary (3 operands) exp1 ? exp2 : exp3

(C) Copyright 2020 by Prof.Abeer El_korany


8
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF
ans
+ addition ans = 7 + 3; 10

- subtraction ans = 7 - 3; 4

* multiplication ans = 7 * 3; 21

/ division ans = 7 / 3; 2

% modulus ans = 7 % 3; 1

(C) Copyright 2020 by Prof.Abeer El_korany


9
Program 3-27 with Hand Trace Chart

int num1, num2, num3, avg;

cout << "Enter the first number: ";


cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;
avg = (num1 + num2 + num3) / 3;
cout << "The average is " << avg << endl;
return 0;
20

(C) Copyright 2020 by Prof.Abeer El_korany


10
Program 3-27 with Hand Trace Chart

int num1, num2, num3, avg;

cout << "Enter the first number: ";


cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;
avg = (num1 + num2 + num3) / 3;
cout << "The average is " << avg << endl; 20
return 0;
20

(C) Copyright 2020 by Prof.Abeer El_korany


11
A Closer Look at the / Operator
• / (division) operator performs integer division if both
operands are integers
cout << 11 / 4 ;
cout << 11.0 / 4.0 ;
• If either operand is floating point, the result is floating point
cout << 11 / 4.0 ;
cout << 15.0 / 3;
11 / 4 has value 2
11.0 / 4.0 has value 2.75
11 / 4.0 has value 2.75
15.0 / 3 has value 5.0

(C) Copyright 2020 by Prof.Abeer El_korany


12
A Closer Look at the % Operator
• % (modulus) operator computes the
remainder resulting from integer division
cout << 13 % 5; // displays 3
• % requires integers for both operands
cout << 13 % 5.0; // error

(C) Copyright 2020 by Prof.Abeer El_korany


13
Arithmetic Operators
• Common
– Addition +
– Subtraction -
– Multiplication * Write m*x + b
not mx + b
– Division /
– Mod %

• Compound assignation operators


(shorthand) (+=, -=, *=, /=, %=)
• Increase and decrease
(C) Copyright 2020 by Prof.Abeer El_korany
14
Arithmetic Operators Example
#include <iostream>
using namespace std;
int main() // tests operators +, -, *, /, and %:
{
int m=54;
int n=20;
cout << "m = " << m << " and n = " << n << endl;
cout << "m+n = " << m+n << endl; 54-20 = 34
cout << "m-n = " << m-n << endl; 54-20 = 34
cout << "m*n = " << m*n << endl; 1080
cout << "m/n = " << m/n << endl; 54/20 = 2
cout << "m%n = " << m%n << endl; 54%20 = 14
return 0;
}
(C) Copyright 2020 by Prof.Abeer El_korany
15
(C) Copyright 2020 by Prof.Abeer El_korany
16
Compound assignation operators
(shorthand)
(+=, -=, *=, /=, %=)
Example
• value += incr; is equivalent to value = value + incr;
• a -= 5; is equivalent to a = a - 5;
• a /= b; is equivalent to a = a / b;
• pr *= units + 1; is equivalent to pr = pr* (units + 1);

NOTE: x *= y + 2 means x = x*(y + 2)


and NOT x = x*y + 2.
(C) Copyright 2020 by Prof.Abeer El_korany
17
Arithmetic Operators
Shortcut assignment

(C) Copyright 2020 by Prof.Abeer El_korany


18
#include <iostream> Compound assignation
using namespace std; operators example
int main() N=13
{ // tests arithmetic assignment operators: N=17
int n=40; N=12
n /= 3; // divides n by 3 N=24
cout << " N = " << n << endl; N=3
n += 4; // adds 4 to n

cout << " N = " << n << endl;


n -= 5; // subtracts 5 from n

cout << "N = " << n << endl;


n *= 2; // multiplies n by 2

cout << "N = " << n << endl;


n %= 7; // reduces n to the remainder from dividing by 7

cout << "N = " << n << endl;


return 0;
19 (C) Copyright 2020 by Prof.Abeer El_korany
Increase and decrease

a++; a=a+1; a+=1;


are all equivalent in its functionality:
the three increase the value of a by 1

• A characteristic of this operator is that it can


be used both as a prefix or as a suffix. That
means it can be written before the variable
identifier (++a) or after (a++)

(C) Copyright 2020 by Prof.Abeer El_korany


20
Increase and decrease (cont.)
• a prefix (++a) the value is increased before the
expression is evaluated
• suffix (a++) the value stored in a is increased
after being evaluated

Example 1 Example 2
A=1, B=3; A=1, B=3;
A=++B; // A is 4, B is 4 A=B++;// A is 3, B is 4
Example 1, B is increased before its value is assigned to A.
Example 2, the value of B is assigned to A and B is later
increased.

(C) Copyright 2020 by Prof.Abeer El_korany


21
include <iostream> a = 10
using namespace std; and a++ = 11
int main() ++a = 12
B++=21
{
B = 21
int a=10;
int b=20;
cout << "a = " << a ++ <<endl;
cout<< "and a++ = " << a << endl;
cout<<"++a = " << ++a << endl;
cout << "b++= " << ++b << " and b = " << b << endl;
return 0;
}

(C) Copyright 2020 by Prof.Abeer El_korany


22
Grouping with Parentheses

(C) Copyright 2020 by Prof.Abeer El_korany


23
Arithmetic Expression
Precedence Rules
1) Brackets : Left to right, inner to
outer
2) Unary, Multiplication, division,
modulus: left to right
3) Addition and subtraction : Left to
right
Binary Operators in the same level (such as + and -)
are of equal priority and are evaluated left to right.
Unary Operators in the same level (such as + and -)
are of equal priority and are evaluated right to left.
24
(C) Copyright 2020 by Prof.Abeer El_korany
Arithmetic Expression
Evaluate the expression
z – (a + b / 2) + w * -y
Given z = 8, a = 3, b = 9, w = 2, y = -5
8 – (3 + 9 / 2) + 2 * - -5
(Step-1) 9/2 = 4
8 – (3 + 4) + 2 * - -5
(Step-2) (3+4) = 7
8 – 7 + 2 * - -5
(Step-3) --5=5
8–7+2*5
(Step-4) 2 * 5 = 10
8 – 7 + 10
(Step-5) 8–7=1
1 + 10
(Step-6) 1 + 10 = 11 25
11
(C) Copyright 2020 by Prof.Abeer El_korany
Arithmetic Operator Precedence
Example
20 - 4 * 5 / 2 + 3 * 5 % 4
(4 * 5)
((4 * 5) / 2)
((4 * 5) / 2) (3 * 5)
((4 * 5) / 2) ((3 * 5) % 4)
(20 ((4 * 5) / 2)) + ((3 * 5) % 4)
(20 -((4 * 5) / 2)) + ((3 * 5) % 4)

(C) Copyright 2020 by Prof.Abeer El_korany


26
Arithmetic functions
• Math Library Functions
• Include the header file <math>
• Functions called by writing
– functionName (argument);

• Three frequently used math function:

– fabs(x) return the absolute value of a number


– pow(x,y) return the power of a number (second argument)
– sqrt(x) return square root of positive number

(C) Copyright 2020 by Prof.Abeer El_korany


27
Arithmetic functions Example
#include <iostream>
#include <math>
using namespace std;
int main()
{
int index =-23;
int x=5; int y=3;
cout << "The absolute value of index is "<< fabs(index);
cout<<“\n The value of " << x<< " to the power " <<y<<" is "
<<pow(x,y)<<endl;
return 0;
}

(C) Copyright 2020 by Prof.Abeer El_korany


28
More Mathematical Library Functions
• Require cmath header file
• Take double as input, return a double
• Commonly used functions:
sin Sine
cos Cosine
tan Tangent
sqrt Square root
log Natural (e) log
abs Absolute value (takes and returns an int)

(C) Copyright 2020 by Prof.Abeer El_korany


29
Relational (Comparison) Operators
• ( ==, !=, >, <, >=, <= )
• the result of a comparison operation is a bool
value that can only be true or false, according to
the result of the comparison.
• A warning: Beware of using ''='' (assignment
operation) instead of '' =='', (comparison
operation)
Example:
a=2, b=3;
((b=2) ==a); would return ???
(C) Copyright 2020 by Prof.Abeer El_korany
30
Relational Operators Example
Assume that a program initializes four variables as follows:
int a=5;
int b=10;
int c=15;
int d=5;
The following statements are then:
a == b; False
b > c; False
d < b;
True
a != d;
(a*b >= c) False
(c+4 > a+b) True
true
(C) Copyright 2020 by Prof.Abeer El_korany
31
Relational Operators Example
Assume that a program initializes four variables as follows:
int a=5;
int b=10;
int c=15;
int d=5;
The following statements are then:
a == b; False
b > c; False
d < b;
True
a != d;
(a*b >= c) False
(c+4 > a+b) True
true
(C) Copyright 2020 by Prof.Abeer El_korany
32
Logical Operators ( || ,&& ,!)
• Logical operators are usually used with conditional
statements
– Operator ! is equivalent to boolean operation NOT
– operators && is equivalent to boolean logic operations
AND
– operators || is equivalent to boolean logic operations OR.
a b a && b a || b
true true true true
true false false true
false true false true
false false false false
Truth table
(C) Copyright 2020 by Prof.Abeer El_korany
33
Order of Precedence
• If we have the expression: a + b * c
• We may want the effect as either (a + b) * c or a + (b * c)
• All operators have a priority, and high priority operators are
evaluated before lower priority ones.
• Operators of the same priority are evaluated from left to
right () [ ]
++ -- increment/decrement
! Logical NOT
* / % arithmetical operations
+ - arithmetical operations
< <= > >= Relational operators
== != Relational operators
&& || Logic operators
= += -= *= /= %= Assignation
>>= <<= &= ^= |=
• a < 10 && 2 * b < c is equivalent to ( a < 10 ) && ( ( 2 * b ) < c )
(C) Copyright 2020 by Prof.Abeer El_korany
34
what is the result of the following mathematical expressions:

int X = 40 , Y= 10 , Z = 6,W;
W= X-Y/4 + Z++;
44

cout<<setw(6)<<W<<endl;
21
cout<<setw(6)<<Z<<endl;
W= ++X/2 + Z%2;
cout<< setw(6)<<X<<endl;
cout<<setw(6)<< W<<endl;
(C) Copyright 2020 by Prof.Abeer El_korany
35

You might also like