Logical operators in C allow you to combine or invert conditions, providing a way to perform more complex logical expressions.
They are often used in decision-making statements, such as if, while, and for loops, to control program flow based on multiple conditions.
1. Logical Operators in C
Operator | Symbol | Description | Example |
---|---|---|---|
AND | && | Returns true if both conditions are true | a && b |
OR | ` | ` | |
NOT | ! | Inverts the condition's result | !a |
2. Logical AND Operator (&&)
The && operator evaluates to true (1) if both conditions are true. If either condition is false, the result is false (0).
Example: Using && to Check Multiple Conditions
#include <stdio.h> int main() { int age = 20; int income = 35000; if (age >= 18 && income >= 30000) { printf("Eligible for loan\n"); } else { printf("Not eligible for loan\n"); } return 0; }
In this example:
- The condition age >= 18 && income >= 30000 checks if both age is at least 18 and income is at least 30000.
- Only if both are true will it print “Eligible for loan”.
Output (assuming age is 20 and income is 35000):
Eligible for loan
3. Logical OR Operator (||)
The || operator evaluates to true (1) if at least one of the conditions is true.
If both conditions are false, it returns false (0).
Example: Using || to Allow Alternative Conditions
#include <stdio.h> int main() { int age = 17; int parentConsent = 1; // 1 means consent is given, 0 means no consent if (age >= 18 || parentConsent) { printf("You can attend the event\n"); } else { printf("You cannot attend the event\n"); } return 0; }
In this example:
- age >= 18 || parentConsent allows attendance if age is 18 or older or if there is parental consent.
- This condition is true if either age is 18 or more, or parentConsent is 1.
Output (assuming age is 17 and parentConsent is 1):
You can attend the event
4. Logical NOT Operator (!)
The ! operator negates a condition. If the condition is true, ! makes it false, and if it is false, ! makes it true.
Example: Using ! to Invert a Condition
#include <stdio.h> int main() { int isWeekend = 0; // 0 means it's a weekday if (!isWeekend) { printf("It's a weekday. Go to work!\n"); } else { printf("It's the weekend. Time to relax!\n"); } return 0; }
In this example:
- !isWeekend inverts isWeekend, so if isWeekend is 0 (false), !isWeekend becomes true.
- This makes the condition true only on weekdays.
Output (assuming isWeekend is 0):
It's a weekday. Go to work!
5. Combining Logical Operators
Logical operators can be combined in complex conditions.
To control the order of evaluation, you can use parentheses.
Example: Using Combined Logical Operators in a Complex Condition
#include <stdio.h> int main() { int age = 25; int hasID = 1; int member = 0; if ((age >= 18 && hasID) || member) { printf("Access granted\n"); } else { printf("Access denied\n"); } return 0; }
In this example:
- (age >= 18 && hasID) || member allows access if either the person is at least 18 and has an ID, or they are a member.
- Parentheses ensure that age >= 18 && hasID is evaluated first, and then it checks if the result is true or if member is true.
Output (assuming age is 25, hasID is 1, and member is 0):
Access granted
6. Short-Circuit Evaluation
C uses short-circuit evaluation for logical operators:
- In an && operation, if the first condition is false, the second condition is not evaluated, as the result will be false.
- In an || operation, if the first condition is true, the second condition is not evaluated, as the result will be true.
Example: Short-Circuit Evaluation with &&
#include <stdio.h> int main() { int a = 0; int b = 10; if (a != 0 && (b / a) > 5) { printf("Condition is true\n"); } else { printf("Condition is false\n"); } return 0; }
In this example:
- a != 0 is false, so (b / a) > 5 is never evaluated, avoiding a division by zero error.
Output:
Condition is false
7. Logical Operators in while Loops
Logical operators are frequently used in loops to create complex exit conditions.
Example: Using Logical Operators in a while Loop
#include <stdio.h> int main() { int count = 0; int limit = 10; printf("Counting until either limit is reached or count reaches 5:\n"); while (count < limit && count < 5) { printf("Count: %d\n", count); count++; } return 0; }
In this example:
- The loop condition count < limit && count < 5 stops the loop if count reaches 5 or limit is reached.
Output:
Counting until either limit is reached or count reaches 5: Count: 0 Count: 1 Count: 2 Count: 3 Count: 4
8. Summary of Logical Operators in C
Operator | Description | Example | Result (assuming a = 10 and b = 20) |
---|---|---|---|
&& | AND – True if both operands are true | a > 5 && b > 15 | 1 (true) |
` | ` | OR – True if at least one operand is true | |
! | NOT – Inverts the truth value of an operand | !(a > b) | 1 (true, as a is not greater than b) |
9. Complete Example Program: Combining Logical Operators
This program checks if a student passes a course based on their scores and attendance.
#include <stdio.h> int main() { int score; int attendance; printf("Enter the student's score (out of 100): "); scanf("%d", &score); printf("Enter the student's attendance (out of 100): "); scanf("%d", &attendance); if (score >= 50 && attendance >= 75) { printf("The student has passed the course.\n"); } else if (score < 50 && attendance < 75) { printf("The student has failed due to low score and low attendance.\n"); } else if (score < 50) { printf("The student has failed due to low score.\n"); } else if (attendance < 75) { printf("The student has failed due to low attendance.\n"); } return 0; }
In this example:
- score >= 50 && attendance >= 75 checks if both the score and attendance are sufficient to pass.
- Further conditions detail why the student may have failed (low score, low attendance, or both).
Example Input/Output:
For inputs score = 45 and attendance = 80:
Enter the student's score (out of 100): 45 Enter the student's attendance (out of 100): 80 The student has failed due to low score.
This tutorial covers the basics of logical operators in C, explaining how they work individually and how they can be combined to create complex conditions.