Logical operators are used to determine the logic between variables or values:
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns 1 (true) because 5 is greater than 3 AND 5 is less than 10
printf("%d", x > 3 && x < 10);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x != y); // returns 1 (true) because 5 is not equal to 3
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns 1 (true) because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
printf("%d", x > 3 || x < 4);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns false (0) because ! (not) is used to reverse the result
printf("%d", !(x > 3 && x < 10));
return 0;
}