**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.

# Logical AND :

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

- 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.

#### Truth table for Logical AND :

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

T | T | T |

T | F | F |

F | T | F |

F | F | F |

# Logical OR :

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

- 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.

#### Truth table for Logical AND :

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

F | F | F |

F | T | T |

T | F | T |

T | T | T |

# Logical NOT :

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

- 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.

#### Truth table for Logical AND :

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

T | F |

F | T |

**De Morgan's Rule :**

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

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

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

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

> >= < <=

== !=

&&

|| Lowest

**Example : The value is !(3 && 4 >= 7 || 6) is 1**

## 0 Komentar untuk "Logical Operators in C Languages"