Logical Operators
Logical operators normally take relational expressions as operands. The ! operator takes one operand. The rest take two: one to the left, and one to the right.
&& | AND |
|| | OR |
! | NOT |
Logical Expressions
expression1 && expression2 is true if, and only if, both expressions are true. |
expression1 || expression2 is true if either one or both expressions are true. |
!expression is true if the expression is false, and vice versa. |
Order of Evaluation for Logical Expressions
Logical expressions are evaluated from left to right. Evaluation stops as soon as something is discovered that renders the expression false.
Examples
6 > 2 && 3 == 3 is true. |
! ( 6 > 2 && 3 == 3 ) is false. |
x != 0 && 20/x < 5 The second expression is evaluated only if ... |
Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.