Loading lessons...
Logical Operators
Logical Operators
Logical operators combine true/false values into one decision: && (AND), || (OR), and ! (NOT).
AND: both must be true
1 && 1 is the only AND that is true:
1 && 1-> 11 && 0-> 00 && 1-> 00 && 0-> 0
OR: at least one must be true
1 || 1-> 11 || 0-> 10 || 1-> 10 || 0-> 0
NOT flips the value
!1-> 0!0-> 1
Examples
int raining = 1;
int holiday = 0;
printf("%d\n", raining && holiday); // 0
printf("%d\n", raining || holiday); // 1
printf("%d\n", !raining); // 0
Short-circuit evaluation
For a && b, if a is already 0, the result is 0 - so b is never even evaluated. For a || b, if a is 1, b is skipped.
TL;DR
&&is true only when both sides are true.||is true when at least one side is true.!flips true to false and false to true.- C short-circuits: it skips the rest when it already knows the answer.