C Logical Operators in Conditions
1. What are Logical Operators?
Logical operators are used to evaluate true/false conditions. In C, the main logical operators are:
| Operator | Symbol | Description |
|---|---|---|
| AND | && |
True if both conditions are true |
| OR | ` | |
| NOT | ! |
Inverts the boolean value (true → false, false → true) |
Logical operators always return 1 (true) or 0 (false).
2. Logical AND (&&)
-
True only if both conditions are true.
Output:
Both
age >= 18andhasTicketmust be true.
3. Logical OR (||)
-
True if at least one condition is true.
Output:
Only one of the conditions needs to be true.
4. Logical NOT (!)
-
Inverts the boolean value: true → false, false → true
Output:
!isRainingis true becauseisRainingis 0 (false).
5. Combining Logical Operators
-
You can combine multiple operators in a single condition.
Output:
Parentheses are used to control evaluation order.
6. Key Points
-
&&→ all conditions must be true -
||→ at least one condition must be true -
!→ reverses the boolean value -
Combine logical operators for complex conditions
-
Always use parentheses for clarity when mixing operators
