Lecture 7 relational_and_logical_operators
-
Upload
eshikshak -
Category
Technology
-
view
588 -
download
0
description
Transcript of Lecture 7 relational_and_logical_operators
Relational and Logical Operators
www.eshikshak.co.in
Relational Operators● Compare two values
● Six Operators, No space between the operator
Result1 True0 False
Operator Meaning== Equal to!= Not Equal to< Less than> Greater than<= Less than or equal to>= Greater than or equal to
www.eshikshak.co.in
● If arithmetic expression on either side of relational operator, than arithmetic expression will be evaluated first and then the results are compared.
● All of these operators are known as binary operator, as they required two expressions as operands.
● They are used with condition statements
Relational Operators
www.eshikshak.co.in
Example● if ( value == 0 )● {● printf (“The value you entered was zero.\n”) ;● printf(“Please try again.\n”) ;● }● else● {● printf (“Value = %d.\n”, value) ;● }
www.eshikshak.co.in
Relational Operators
Expression Meaning Result
5 == 3 5 is equal to 3 0
5 != 3 5 is not equal to 3 1
5 > 3 5 is greater than 3 1
5 < 3 5 is less than 3 0
5 >= 3 5 is greater than or equal to 3 1
5 <= 3 5 is less than or equal to 3 0
www.eshikshak.co.in
Logical Operator● Sometimes we need to test multiple
conditions in order to make a decision.● Logical operators are used for combining
simple conditions to make complex conditions.
www.eshikshak.co.in
Logical Operator
Operator Meaning
&& AND
|| OR
! NOT
www.eshikshak.co.in
&& (Logical AND)
● (expression1 && expression2)●
expression1 expression2 Overall Result
True(non-zero)
True(non-zero)
True(non-zero)
True(non-zero)
False(zero)
False(zero)
False(zero)
True(non-zero)
False(zero)
False(zero)
False(zero)
False(zero)
www.eshikshak.co.in
&& (Logical AND)
Expression Result
A = ( 5 != 4 ) && ( 3 + 6 == 9 ) A = 1
A = ( 5 == 4 ) && ( 3 + 6 == 9 ) A = 0
A = ( 5 == 5 ) && ( 3 + 5 == 9 ) A = 0
A = ( 5 == 4 ) && ( 3 + 5 == 9 ) A = 0
www.eshikshak.co.in
● (expression1 || expression2)
|| (Logical OR)
expression1 expression2 Overall Result
True(non-zero)
True(non-zero)
True(non-zero)
True(non-zero)
False(zero)
True(non-zero)
False(zero)
True(non-zero)
True(non-zero)
False(zero)
False(zero)
False(zero)
www.eshikshak.co.in
|| (Logical OR)
Expression Result
A = ( 5 != 4 ) || ( 3 + 6 == 9 ) A = 1
A = ( 5 == 4 ) || ( 3 + 6 == 9 ) A = 0
A = ( 5 == 5 ) || ( 3 + 5 == 9 ) A = 0
A = ( 5 == 4 ) || ( 3 + 5 == 9 ) A = 1
www.eshikshak.co.in
www.eshikshak.co.in