c / beginner
Snippet
Logical Operators
Logical operators allow you to combine multiple boolean expressions. In C, 0 is false and any non-zero value (usually 1) is true. '&&' is AND, '||' is OR, and '!' is NOT.
snippet.c
1
2
3
4
5
6
int isAdult = 1;int hasTicket = 0;int canEnter = isAdult && hasTicket;int canWait = isAdult || hasTicket;int isBlocked = !isAdult;
Breakdown
1
int canEnter = isAdult && hasTicket;
Result is 1 only if both variables are true (non-zero).
2
int isBlocked = !isAdult;
Inverts the value; if isAdult is 1, isBlocked becomes 0.