Introduction
Logical operators help you make decisions by combining true or false conditions.
expression1 && expression2 // AND operator expression1 || expression2 // OR operator !expression // NOT operator
if (age >= 18 && hasID) { // code runs if age is 18 or more AND hasID is true }
if (isSunny || isWeekend) { // code runs if it is sunny OR it is weekend }
if (!isRaining) { // code runs if it is NOT raining }
#include <stdio.h> int main() { int age = 20; int hasID = 1; // 1 means true, 0 means false int isSunny = 0; int isWeekend = 1; if (age >= 18 && hasID) { printf("You can enter the club.\n"); } else { printf("You cannot enter the club.\n"); } if (isSunny || isWeekend) { printf("You can go outside.\n"); } else { printf("Better stay inside.\n"); } if (!isSunny) { printf("Don't forget your umbrella!\n"); } return 0; }
int a = 5, b = 10;
if (a > 3 && b < 15) {
printf("True\n");
} else {
printf("False\n");
}
int x = 0;
if (x = 5 || x == 0) {
printf("Yes\n");
}
int a = 0, b = 1, c = 2;
if (!(a || b) && c > 1) {
printf("Inside if\n");
} else {
printf("Inside else\n");
}
Pass the test and this pattern moves unknown