![]() |
VOOZH | about |
Logical operators in C are used to combine multiple conditions and evaluate them as a single expression. They are commonly used in decision-making statements such as if, if-else, while, and for loops to control program flow.
C provides three logical operators:
The logical AND operator (&&) returns true only if both operands are non-zero. Otherwise, it returns false (0). The return type of the result is int. Below is the truth table for the logical AND operator.
X | Y | X && Y |
|---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Syntax
(operand_1 && operand_2)
Both values are greater than 0
The logical OR operator returns true if any one of the operands is non-zero. Otherwise, it returns false i.e., 0 as the value. Below is the truth table for the logical OR operator.
| X | Y | X || Y |
|---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Syntax
(operand_1 || operand_2)
Any one of the given value is greater than 0
The logical NOT operator (!) reverses the result of a condition. If the condition is true, it becomes false, and if the condition is false, it becomes true. Below is the truth table for the logical NOT operator.
| X | !X |
|---|---|
0 | 1 |
1 | 0 |
Syntax
!(operand_1 && operand_2)When the result can be determined by evaluating the preceding Logical expression without evaluating the other operands, it is known as short-circuiting.
Short-circuiting can be seen in the equation having more than one Logical operator. They can either AND, OR, or both.
The logical AND operator returns true only if both operands evaluate to true. If the first operand is false, the second operand is not evaluated because the overall result will always be false. This is because even if the further operands evaluate to true, the entire condition will still return false.
10
OR operator returns true if at least one operand evaluates to true. If the first operand is true, the second operand is not evaluated because the overall result is already true. This is because even if the further operands evaluate to false, the entire condition will still return true.
This will be printed 10