Lecture 5 GÇô Operators and Expressions
Lecture 5 GÇô Operators and Expressions
– Arithmetic operators
– Relational operators
– Logical operators
– Assignment operators
– Increment and decrement operators
– Conditional operators
3
Arithmetic Operators
• C++ provides five basic arithmetic operators.
Operator Name Example Result
+ Addition 12 + 4.9 16.9
Modulo division
% (Remainder)
13 % 3 1
4
Arithmetic Operators
• Except modulo division (%), all the arithmetic
operators can accept a mix of integer and real
operands.
5–3 2
5*3 15
5 /3 1
5%3 2
6
Integer Arithmetic
• During modulo division, the sign of the result
is always the result of the first operand
Expressions Result
-14 % 3 -2
-14 % -3 -2
14 % -3 2
7
Real Arithmetic
• An arithmetic operation involving real
operands is called real arithmetic.
• A real operand may assume values either in
decimal or exponential notation.
Expressions Result
X = 6.0 / 7.0 0.857143
Y = 1.0 / 3.0 0.333333
Expressions Result
15/10.0 1.5
15/10 1
9
Relational Operators
• Relational operators use to compare numeric quantities.
• Relational operators evaluate to 1 / T or 0 / F result.
• For example:
– 'A' < 'F'
• Example:
– (12 * 5) > (10 + 5) Relational Expression
– 60 > 15
A B R A B R
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1
Logical NOT
A R
0 1
1 0
16
Example: Logical Operators
#include<iostream> Logical AND operator
using namespace std; is used to combine two
int main() relational expressions
{
int marks = 48, attendance = 65;
if(marks>45 && attendance>80)
{
cout<<"Eligible for resit exam ";
}
else
{
cout<<"Not eligible for resit exam";
}
} 17
Assignment Operators
• Assignment operators are used to assign values to
variables.
-= n - = 25 n = n - 25
*= n *= 25 n = n * 25
/= n /= 25 n = n / 25
%= n %= 25 n = n % 25
20
Assignment Operators
• Example:
int m, n, p;
m = n = p = 100;
m = (n = p = 100) + 2;
• Method: floor(x)
• Description :Rounds x to the largest integer not
greater than x
• Example: floor(9.2)
– Result : 9.0 29
Mathematical Functions
• Method: abs(x)
• Description: The argument of this function is an
integer. It calculates the absolute value of the
integer.
• Example: abs(-25)
– Result: 25
• Method: fabs(x)
• Description : The argument of this function is
either float or double. It returns the absolute value
of a floating point number.
• Example: fabs(-8.76)
– Result : 8.76
30
Mathematical Functions
• Method: sqrt(x)
• Description : Square root of x
• Example: sqrt(900.0)
– Result : 30.0
• Method: pow(x,y)
• Description: x raised to power y (xy)
• Example: pow(2,7)
– Result:128
31
What is the Output?
#include<iostream>
using namespace std;
int main()
{
int a= -15%2;
int b= -14%2;
cout<< a;
cout<<endl<<b;
}
-1
0
32
What is the Output?
#include<iostream>
using namespace std;
int main()
{
int a= ('x'<'y');
cout<< a;
}
33
What is the Output?
#include<iostream>
using namespace std;
int main()
{
int n = 10, m = 20;
int x;
x = (n<m) ? n + 2 : m * 4;
cout<<"Value of x = "<<x;
}
12
34
What is the Output?
#include<iostream>
using namespace std;
int main()
{
int m, y;
m = 5;
y = m++;
cout<<m;
cout<<endl<<y;
}
6
5
35
What is the Output?
#include<iostream>
using namespace std;
int main()
{
int m=10;
cout<<m++<<endl;
cout<<m<<endl;
cout<<++m<<endl;
}
10
11
12
36
What is the Output?
#include<iostream>
using namespace std;
int main()
{
int m=10;
cout<<++m<<endl;
cout<<m++<<endl;
cout<<m<<endl;
}
11
11
12
37
THANK YOU
38