Combining Conditions: &&, || and !

Sometimes one question isn't enough. You can join conditions with logical operators:

OperatorNameTrue when...
&&ANDboth sides are true
||ORat least one side is true
!NOTthe condition is false (it flips it)

&& (and)

#include <stdio.h>

int main() {
    int age = 25;
    int hasTicket = 1;
    if (age >= 18 && hasTicket) {
        printf("Welcome to the concert!\n");
    }
    return 0;
}

Output

Welcome to the concert!

|| (or)

#include <stdio.h>

int main() {
    char day = 'S';
    if (day == 'S' || day == 'U') {
        printf("Weekend!\n");
    } else {
        printf("Work day.\n");
    }
    return 0;
}

Output

Weekend!

! (not)

#include <stdio.h>

int main() {
    int isRaining = 0;
    if (!isRaining) {
        printf("Let's go for a walk.\n");
    }
    return 0;
}

Output

Let's go for a walk.

Think of it like this: && is like a door with two locks: you need both keys. || is like a house with two doors: either one gets you in.

Checking a range

To check that a number is between two values, you need &&:

#include <stdio.h>

int main() {
    int hour = 14;
    if (hour >= 9 && hour < 17) {
        printf("The shop is open.\n");
    }
    return 0;
}

Output

The shop is open.

Common mistake: Writing 9 <= hour <= 17 like in maths. C does not read it that way – it gives the wrong answer. Always write two comparisons joined with &&.

HOME LABS COMMUNITY DASHBOARD