|| JAI SRI GURUDEV ||
SRI ADICHUNCHANAGIRI SHIKSHANA TRUST ®
ADICHUNCHANAGIRI INSTITUTE OF TECHNOLOGY
(An ISO 9001 : 2008 Certified)
Affiliated to Visvesvaraya Technological University, Belagavi ,
Approved by AICTE and Accredited by NAAC, New Delhi
Jyothinagar, Chikkamagaluru – 577 102.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
ONLINE CLASS USING ZOOM APPLICATION
Subject: ‘C’ Programming for Problem Solving
Subject Code: 18CPS23
Presented By: Semester and Section : 2nd and ‘B’
Mr. GOPINATH C. B., B.E., M.Tech., Class # : 5
Assistant
8 April 2020 Professor Date : 08-04-2020 (11:00AM To 12:00PM)
Logical Operators
The different Logical Operators are
Operator Meaning Precedence Associativity
! Logical NOT 1 Right to Left
&& Logical AND 2 Left to Right
|| Logical OR 3 Left to Right
The Logical Operators && and || are used when we want to
test more than one condition and make decisions.
For Example: (a>b) && (x==10)
A Logical Expressions also provides a value of ONE(1) or
ZERO(0), according to the Truth Table shown below.
Logical Operators (CONTD….)
Truth Table for && (Logical AND) and || (Logical OR) Operators
Value of the Expression
OP1 OP2
OP1 && OP2 OP1 && OP2
0 0 0 0
0 1 0 1
(Non-Zero)
1 0 0 1
(Non-Zero)
1 1 1 1
(Non-Zero) (Non-Zero)
Logical Operators (CONTD….)
Truth Table for ! (Logical NOT) Operator
OP1 !(OP1)
1
0 (Non-Zero)
1
0
(Non-Zero)
Logical Operators (CONTD….)
Some Examples of the Usage of Logical Expressions are:
Example1: if age = 20 and salary = 10000, then
if(age > 55 && salary < 15000)
or
if((age > 55) && (salary < 15000))
if((20 > 55) && (10000 < 15000))
if( 0 && (10000 < 15000))
if( 0 && 1 )
if(0)
Here, the if condition becomes FALSE.
Logical Operators (CONTD….)
Example2: if number = 20, then
if(number < 0 || number > 100)
or
if((number < 0) || (number > 100))
if(( 20 < 0) || ( 20 > 100))
if( 0 || ( 20 > 100))
if( 0 || 0 )
if(0)
Here, the if condition becomes FALSE.
Logical Operators (CONTD….)
Rules for Logical Operator Evaluation:
First evaluate () from Left to Right if any.
Evaluate Unary Operator from Right to Left if any.
Evaluate *, / and % from Left to Right if any.
Evaluate + and – from Left to Right if any.
Evaluate <, <=, > and >= from Left to Right if any.
Evaluate == and != from Left to Right if any.
Evaluate && operator from Left to Right if any.
Evaluate || operator from Left to Right if any.
Finally use Assignment Operator.
Example1: If a=11, b=6, c=0, d=7 and e=5 then evaluate
a + 2 > b || !c && a == d || a - 2 < = e
a + 2 > b || !c && a == d || a - 2 < = e
11 + 2 > 6 || !0 && 11 == 7 || 11 – 2 <= 5
11 + 2 > 6 || 1 && 11 == 7 || 11 – 2 <= 5
13 > 6 || 1 && 11 == 7 || 11 – 2 <= 5
13 > 6 || 1 && 11 == 7 || 9 <= 5
1 || 1 && 11 == 7 || 9 <= 5
1 || 1 && 11 == 7 || 0
1 || 1 && 0 || 0
1 || 0 || 0
1 || 0
1
Example2: If a=11, b=6, c=4, d=7 and e=5 then evaluate
a + 2 > b || !c && a == d || a - 2 < = e
a + 2 > b || !c && a == d || a - 2 < = e
11 + 2 > 6 || !4 && 11 == 7 || 11 – 2 <= 5
11 + 2 > 6 || 0 && 11 == 7 || 11 – 2 <= 5
13 > 6 || 0 && 11 == 7 || 11 – 2 <= 5
13 > 6 || 0 && 11 == 7 || 9 <= 5
1 || 0 && 11 == 7 || 9 <= 5
1 || 0 && 11 == 7 || 0
1 || 0 && 0 || 0
1 || 0 || 0
1 || 0
1
Example3: Evaluate the following expression
22 + 3 <= 6 && !5 || 2 == 7 && 22 – 2 >= 5
22 + 3 <= 6 && !5 || 2 == 7 && 22 – 2 >= 5
22 + 3 <= 6 && 0 || 2 == 7 && 22 – 2 >= 5
25 <= 6 && 0 || 2 == 7 && 22 – 2 >= 5
25 <= 6 && 0 || 2 == 7 && 20 >= 5
0 && 0 || 2 == 7 && 20 >= 5
0 && 0 || 2 == 7 && 1
0 && 0 || 0 && 1
0 || 0 && 1
0 || 0
0
Write the Output of the following code.
void main()
{
int a=5, b=2, res1;
float f1=5.0, f2=2.0, res2;
res1=5/2.0+a/2+a/b;
res2=f1/2*f1-f2;
printf(“res1 = %d and res2 = %f”,res1,res2);
}
OUTPUT:
res1 = 6 and res2 = 10.500000
Conditional Operators
The different Conditional Operators are
Operator Meaning Precedence Associativity
? Question Mark 1 Right to Left
: Colon 1 Right to Left
The Conditional Operator is also called as Ternary
Operator.
Conditional Expression is of the following form
exp1 ? exp2 : exp3
Where exp1, exp2 and exp3 are expressions.
Conditional Operators (CONTD….)
The operator ? : works as follows:
exp1 is evaluated first.
If it is non-zero (true), then the expression exp2 is
evaluated and its value becomes the value of the
expression.
If it is zero (false), then exp3 is evaluated and its
value becomes the value of the expression.
Note that Only One of the expressions (either exp2 or
exp3) is evaluated.
Conditional Operators (CONTD….)
For Example, consider the following statements:
True
x = (a > b) ? a : b;
exp1 exp2 exp3
If the exp1 is True,
then x = a
If the exp1 is False,
then x = b
Conditional Operators (CONTD….)
Example1: consider the following statements:
if a = 10;
b = 15;
x = (a > b) ? a : b;
In this example, x will be assigned the value of b.
This can be achieved using the if..else statements as follows:
if(a > b)
{
x = a;
}
else
{
x = b; Here, x = b That is, x = 15
}
Conditional Operators (CONTD….)
Example2: consider the following statements:
if a = 55;
b = 25;
x = (a > b) ? a : b;
In this example, x will be assigned the value of a.
That is, x = 55
THANK YOU