OPENTech Guides

Posted on 02nd May 2013

Logical operators operates on Boolean expressions or values. The result of that operation will be a Boolean result (True or False). Logical operators in C are:

Operator | Meaning |
---|---|

&& | AND - Result TRUE if Left side and right side operands are both TRUE |

|| | OR - Result TRUE if either Left side or right side operand is True or Both operands are True |

! | NOT - Invert the boolean expression or value. True will change to False and False to True |

Consider three variable a, b and c as below

a = 10;

b = 15;

c = 10;

The table below shows some expressions using above variables and the results.

Expression | Meaning | Result |
---|---|---|

(a = c) && (a < b) | a = c is True; a < b is True - Both expressions are TRUE | TRUE |

(a = c) || (a > b) | a = c is True; a > b is False - One expression is TRUE | TRUE |

!( a > b) | a > b is False; Result is Inverse of False | TRUE |

( a > b) && (c < b) | a > b is False, c < b is True; Left side is FALSE | FALSE |

( a > b) && (c > b) | a > b is False, c > b is False; Both FALSE | FALSE |

!( a = c) | a = c is True; Result is inverse of True | FALSE |

Nothing yet..be the first to share wisdom.