Technologies

C

Logical Operators in C Languages
(adsbygoogle = window.adsbygoogle || []).push({});
# Logical AND :

#### Truth table for Logical AND :

# Logical OR :

#### Truth table for Logical AND :

# Logical NOT :

#### Truth table for Logical AND :

**De Morgan's Rule : **
**Example : The value is !(3 && 4 >= 7 || 6) is 1**
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});

CLanguage

Friday, 31 Mar 2017

68

0

0

**Logical Operators : ** C has the following three logical operators

**Logical AND - &&****Logical OR - ||****Logical NOT - !**

The logical operators **&&** and **||** are used, to test more than one condition and make decisions.

An expression which combines two or more relational expressions is termed as a logical expression or a compound relational expression. Like the simple relational expression, a logical expression also yields value of one or zero. True is taken as 1 and false is taken as 0.

The zero value is taken as false, while the non-zero value is taken as true. ** Logical AND, Logical OR ** are binary operators whereas **Logical NOT** is a unary operator.

The operator **!** will be given the highest priority over **&& and || **.

When equal priority operators are there in an expression, the operators will be evaluated from left to right i.e they follow left to right associativity. **Logical NOT(!)** follows right to left associativity.

The result of logical **AND** expression will be true only, when both the relational expressions are true.

Syntax : exp1 && exp2

Examples :

- If a = 10, b = 5, c = 15 and i = (a > b) && (b < c);

The value of i in this expression will be 1. - If a = 10, b = 5, c = 15 and i = (a < b) && (b < c);

The value of i in this expression will be 0.

exp1 |
exp2 |
exp1 && exp2 |
---|---|---|

T | T | T |

T | F | F |

F | T | F |

F | F | F |

The result of logical **OR** expression will be false only, when both the relational expressions are false.

Syntax : exp1 || exp2

Examples :

- If a = 10, b = 5, c = 15 and i = (a < b) || (b < c);

The value of i in this expression will be 1. - If a = 10, b = 5, c = 15 and i = (a < b) && (b > c);

The value of i in this expression will be 0.

exp1 |
exp2 |
exp1 && exp2 |
---|---|---|

F | F | F |

F | T | T |

T | F | T |

T | T | T |

The result of logical **NOT** expression will be true, if the expression false and vice versa.

Syntax : ! exp1

Examples :

- If a = 10, b = 5, c = 15 and i = !((a < b) && (b < c));

The value of i in this expression will be 1. - If x = 20 and i = !(x == 20)

The value of i will be 0, because x == 20 is true and !i = 0.

Exp |
!exp |
---|---|

T | F |

F | T |

i. !(x && y) -> !x || !y

ii. !(x || y) -> !x && !y

ii. !(x || y) -> !x && !y

Examples :

1. !(2 && 0) = 1 = !2 || !0

2. !(2 || -7) = 0 = !2 && !(-7)

1. !(2 && 0) = 1 = !2 || !0

2. !(2 || -7) = 0 = !2 && !(-7)

Relative precedence of the relational and logical operators is as follows :

Highest
!

> >= < <=

== !=

&&

|| Lowest

> >= < <=

== !=

&&

|| Lowest

Post Article

Upload Youtube Video

CLanguage

Friday, 31 Mar 2017

68

0

0

Popular Post

- Write an algorithm and draw the flowchart to find the largest number among the three numbers?
- Write an algorithm an draw flowchart to find factorial of a number?
- Write an algorithm and draw the flowchart to find whether a given number is even or odd?
- Write an algorithm and draw the flowchart to Swap two integers?
- Write an algorithm an draw the flowchart to compute the average of the three numbers?
- Write an algorithm and draw the flowchart to find the largest number amoug two numbers?
- write a c program to swap two numbers
- How to write a algorithm?
- What is mean by Operator Precedence and Associativity in C Language?
- C Languages Relational Operators

© 2019 - technotecode. All contents are copyright of their authors