C Logical Conditions
In this page:
if (condition1 && condition2) {
// both are true
}
if (condition1 || condition2) {
// at least one is true
}
if (!condition) {
// condition is false
}
Logical Operators in Conditions
Logical operators combine multiple individual conditions into a single expression that an if statement can test, letting a decision depend on more than one factor at once.
Example: Logical Operators in Conditions
// 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 hasLicense and set it to 1
int hasLicense = 1;
// Check whether age >= 18 && hasLicense
if (age >= 18 && hasLicense) {
// Print formatted text to the screen
printf("Can drive");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Combining Conditions with &&
The && (logical AND) operator combines two conditions so the overall expression is true only when both individual conditions are true, commonly used to check that a value falls within a specific range.
Example: Combining Conditions with &&
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 60 && score <= 100
if (score >= 60 && score <= 100) {
// Print formatted text to the screen
printf("In range");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Combining Conditions with ||
The || (logical OR) operator combines two conditions so the overall expression is true when at least one of them is true, useful for checking whether a value matches any of several acceptable options.
Example: Combining Conditions with ||
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare grade and set it to 'A'
char grade = 'A';
// 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
Negating a Condition with !
The ! (logical NOT) operator inverts a single condition, turning a true value false and a false value true, which is often used to read naturally as not in front of a condition's name.
Example: Negating a Condition with !
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare isBanned and set it to 0
int isBanned = 0;
// Check whether !isBanned
if (!isBanned) {
// Print formatted text to the screen
printf("Access allowed");
}
// 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 evaluates && and || using short-circuit evaluation: for &&, if the first condition is false, the second is never evaluated at all, which is commonly used to safely guard against operations like division by zero.
Example: Short-Circuit Evaluation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare divisor and set it to 0
int divisor = 0;
// Check whether divisor != 0 && (10 / divisor > 1)
if (divisor != 0 && (10 / divisor > 1)) {
// Print formatted text to the screen
printf("unreachable");
} else {
// Print formatted text to the screen
printf("Avoided division by zero");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
x > 1 && < 10and leaving out the secondx, which is a syntax error. - Confusing
&&with||, so a condition that should need both checks passes with either. - Forgetting parentheses when mixing
&&and||, since&&binds tighter.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: