C Logical Operators
In this page:
condition1 && condition2 // AND
condition1 || condition2 // OR
!condition // NOT
Logical AND (&&)
&& (logical AND) evaluates to true only when both the left and right conditions are true -- useful whenever an action should only happen if multiple requirements are satisfied at once, like checking a value is both positive and below a limit.
Example: Logical AND (&&)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Declare hasID and set it to 1
int hasID = 1;
// Check whether age >= 18 && hasID
if (age >= 18 && hasID) {
// Print formatted text to the screen
printf("Allowed");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical OR (||)
|| (logical OR) evaluates to true if at least one of its two conditions is true -- useful for accepting any of several valid cases, like checking whether input matches one option or another.
Example: Logical OR (||)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare grade and set it to 'B'
char grade = 'B';
// Check whether grade == 'A' || grade == 'B'
if (grade == 'A' || grade == 'B') {
// Print formatted text to the screen
printf("Good grade");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical NOT (!)
! (logical NOT) flips a condition's truth value: applied to something true it becomes false, and applied to something false it becomes true -- handy for writing a condition as "not X" instead of restructuring the whole check.
Example: Logical NOT (!)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare isLoggedIn and set it to 0
int isLoggedIn = 0;
// Check whether !isLoggedIn
if (!isLoggedIn) {
// Print formatted text to the screen
printf("Please log in");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Short-Circuit Evaluation
C's && and || operators short-circuit: in a && b, if a is already false, b is never evaluated at all, since the overall result is already determined -- this matters when b has side effects or could cause an error if evaluated unnecessarily.
Example: Short-Circuit Evaluation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 0
int a = 0;
// Check whether a != 0 && (10 / a > 1)
if (a != 0 && (10 / a > 1)) {
// Print formatted text to the screen
printf("unreachable");
}
// Print formatted text to the screen
printf("Safe: division skipped");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Combining Logical Operators
Combining several logical operators in one expression without parentheses relies on C's default precedence rules, which can be easy to misjudge -- wrapping each sub-condition in parentheses makes the intended grouping unambiguous at a glance.
Example: Combining Logical Operators
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 20
int age = 20;
// Declare hasTicket and set it to 1
int hasTicket = 1;
// Run this block only if the condition is true
if ((age >= 18) && (hasTicket || age >= 65)) {
// Print formatted text to the screen
printf("Entry allowed");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
&instead of&&, which is a bitwise operator and can behave differently. - Writing
if (x == 1 || 2), which is always true because2is non-zero. - Relying on the right side of
&&or||always running when short-circuiting may skip it.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: