C Logical Operators in Conditions
Logical Operators in Conditions
You can combine or reverse conditions using logical operators.
These work together with if, else, and else if to build more complex decisions.
&&(AND) - all conditions must be true||(OR) - at least one condition must be true!(NOT) - reverses a condition (true→false,false→true)
AND (&&)
Use AND (&&) when both conditions must be true:
Example
Test if a is greater than b, and if c is greater than a:
int a = 200;
int b = 33;
int c = 500;
if (a > b && c > a) {
printf("Both conditions are true\n");
}
OR (||)
Use OR (||) when at least one of the conditions can be true:
Example
Test if a is greater than b, or if a is greater than c:
int a = 200;
int b = 33;
int c = 500;
if (a > b || a > c) {
printf("At least one condition is true\n");
}
NOT (!)
The NOT operator (!) reverses a condition:
- If a condition is
true,!makes itfalse. - If a condition is
false,!makes ittrue.
This is useful when you want to check that something is not the case:
Example
Test if a is not greater than b:
int a = 33;
int b = 200;
if (!(a > b)) {
printf("a is NOT greater than b\n");
}
Real-Life Example
In real programs, logical operators are often used for access control. For example, to get access to a system, there are specific requirements:
You must be logged in, and then you either need to be an admin, or have a high security clearance (level 1 or 2):
Example
bool isLoggedIn = true;
bool isAdmin = false;
int securityLevel = 3; // 1 = highest
if (isLoggedIn && (isAdmin || securityLevel <= 2)) {
printf("Access granted\n");
} else {
printf("Access denied\n");
}
// Try changing securityLevel and isAdmin to test different outcomes:
// securityLevel 1 = Access granted
// securityLevel 2 = Access granted
// securityLevel 3 = Access denied
// securityLevel 4 = Access denied
// If isAdmin = true, access is granted.