0% found this document useful (0 votes)
16 views38 pages

Lecture 5 GÇô Operators and Expressions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views38 pages

Lecture 5 GÇô Operators and Expressions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Lecture 5

Operators and Expressions


Operators
• An operator is a symbol that tells the computer
to perform certain mathematical or logical
manipulations (operations).

• Operators are used in programs to manipulate


data and variables.

• They usually form part of the mathematical or


logical expressions.
2
Operators
• Types of operators in C++ are:

– 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

- Subtraction 3.98 - 4 -0.02

* Multiplication 2 * 3.4 6.8

/ Division 9 / 2.0 4.5

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.

• If one or both of the operands are real then the


result will be a real.

• When both operands of an integer (/) divisions


are integers then the result is an integer.
• For example:
9/2=4
5
Integer Arithmetic
• Both the operands in a single arithmetic
expression are integers
• Integer arithmetic always yields an integer value.
Expressions Result
5+3 8

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

Z = -2.0 / 3.0 -0.666667


8
Mixed-mode Arithmetic
• If one of the operand is real and the other is
integer, the expression is called a mixed-mode
arithmetic expression.
• If either operand is of real type, then the result
is always a real number.

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.

Operator Name Example Result


== Is equal to 5==5 1
!= Is not equal to 5! = 5 0

< Less Than 5<5 0


<= Less Than or Equal 5 <= 5 1
> Greater Than 5 > 5.5 0
>= Greater Than or Equal 6.3 >= 5 1
10
Relational Operators
• The operands of a relational operator must
evaluate to a number.

• Characters are valid operands since they are


represented by numeric values.

• For example:
– 'A' < 'F'

• The result of the above example is true because


ASCII value of A is 65 and ASCII value of F is
70.
11
Relational Operators
• The general syntax of a simple relational
expressions:
exp-1 relational operator exp-2
• exp-1 and exp-2 are arithmetic expressions,
which may be simple constants, variables or
combination of them.
• Example:
Relational Operator,
Greater than or equal to
– (23 * 7) >= (-67 + 89)
Arithmetic
Expressions

• The result of the above expression is true 12


Relational Operators
• When arithmetic expressions are used on either
side of the relational operator, the arithmetic
expressions will be evaluated first and then the
results compared.

• Example:
– (12 * 5) > (10 + 5) Relational Expression
– 60 > 15

• Relational expressions are used in decision


statements.
13
Logical Operators
• Logical operators are using to combine relational
expressions.
• Like the relational operators, logical operators
evaluate to 1 (True) or 0 (false).
• Logical && operator has higher priority over
logical || operator.
Operator Name Example Result
! Logical NOT ! (5 = = 5) 0 / False

|| Logical OR 5 < 6 || 6 < 5 1 / True

&& Logical AND 5 < 6 && 6 <= 6 1 / True


14
Logical Operators
• Logical NOT negates the logical value of its
single operand. If its operand is 1 it produces
0, and if it is 0 it produces 1.

• Logical AND produces 0 if one or both of its


operands evaluate to 0. Otherwise 1 is the
result

• Logical OR produces 0 if both of its operands


evaluate to 0. Otherwise, it produces 1.
15
Logical Operators
Logical AND Logical OR

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.

• Its left operand should be a variable, and its right


operand may be an arbitrary expression.

• The general syntax is:


variable operator expression
• Example:
– number = a + 5; Expression

• It will add the values of variable a and 5, and the


result will be assigned to the variable number.
18
Assignment Operator
• The assignment operator assigns a value to a
variable.
– Example: number = 15;

• The assignment operation always takes place


from right to left.

• Assignment operations are expressions that can be


evaluated.
– Example: y = 5 + (x= 6);

• y is assigned the result of adding 5 and the value


of another assignment expression (x = 6).
19
Assignment Operators
Operator Example Equivalent To
= n = 25 -
+= n += 25 n = n + 25

-= 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;

• In the first assignment statement value of m, n


and p is equal to 100.

• In the second assignment statement the value


of m is equal to 102 and the value of n and p is
equal to 100.
21
Increment and Decrement Operators
• The auto increment (++) and auto decrement (--)
operators provide a convenient way of adding
and subtracting 1 from a numeric variable.
• In these examples, value of k = 5
Operator Name Example Result

++ Auto Increment (prefix) ++k + 10 16

++ Auto Increment (postfix) k++ + 10 15

-- Auto Decrement (prefix) --k + 10 14

-- Auto Decrement (postfix) k-- + 10 15 22


Increment and Decrement Operators
• Example:
– m = 5;
– y = ++m;
• A prefix operator first adds 1 to the operand and then
the result is assigned to the variable on left.
• In this case, the value of y and m would be 6.

• If we write the above statements as:


– m = 5;
– y = m++;
• A postfix operator first assigns the value to the
variable on the left and then increments the operand.
• The value of y would be 5 and the value of m would
be 6.
23
Conditional Operator (?)
• The conditional operator takes three operands.
• The general syntax is:
expression-1? expression-2 : expression-3
• The operator ?: works as follows: expression-1 is
evaluated first. If it is true, then the expression-2 is
evaluated. If expression-1 is false, expression-3 is
evaluated.
• Example:
– a = 10;
– b = 15;
– x = (a>b) ? a : b;
• In this example, the value of x will be the value of b.
24
Arithmetic Expressions
• Arithmetic expression is a combination of operands and
operators arranged as per the syntax of the language.

• Expressions are evaluated using an assignment statement.

• The general syntax is:


variable = expressions;

• When the statement is encountered, the expression is


evaluated first and the result then replaces the previous
value of the variable on the left-hand side.
25
Arithmetic Expressions
• Arithmetic expressions without parenthesis will be
evaluated from left to right using the rules of
precedence of operators.
• There are two distinct priority levels of arithmetic
operators:
– High priority * / %
– Low priority + -
• The basic evaluation procedure includes two left to
right passes through the expressions.
• During the first pass, the high priority operators are
applied as they are encountered.
• During the second pass, the low priority operators are
applied as they are encountered.
26
Arithmetic Expressions
• Example:
– x = a - b / 3 + c * 2 –1
– where a = 9, b = 12, c = 3

• This statement is evaluated as follows:


– First Pass:
• Step 1: x = 9 – 4 + 3 * 2 -1
• Step 2: x = 9 – 4 + 6 - 1
– Second Pass:
• Step 3: x = 5 + 6 - 1
• Step 4: 11-1
• Step 5: 10
27
Arithmetic Expressions
• Introducing parentheses into an expression can
change the order of evaluation.
• Example: 9 – 12 / ( 3 + 3 ) * ( 2 – 1 )
• Whenever parentheses are used, the expressions
within the parentheses assume highest priority.
• This statement is evaluated as follows:
– First Pass:
• Step 1: 9 - 12 / 6 * 1
• Step 2: 9 - 2 * 1
– Second Pass:
• Step 3: 9 – 2
• Step 4: 7
28
Mathematical Functions
• The header file #include<math.h> should be
used.
• Method: ceil(x)
• Description : Rounds x to the smallest integer not
less than x
• Example: ceil(9.2)
– Result :10.0

• 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

You might also like