C Academy · Lesson

Relational and Logical

Comparisons and &&, ||.

Lesson 2 of 413 steps

Relational and Logical is a free C Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

Comparing Values

Relational operators compare two values and produce a truth value: 1 for true and 0 for false.

  • < less than
  • > greater than
  • <= less or equal
  • >= greater or equal
#include <stdio.h>
int main(void) {
    printf("3 < 5 = %d\n", 3 < 5);
    printf("3 > 5 = %d\n", 3 > 5);
    return 0;
}

Equality and Inequality

Use == to test equality and != to test inequality. Do not confuse == with the assignment operator =.

#include <stdio.h>
int main(void) {
    int a = 4, b = 4;
    printf("a == b = %d\n", a == b);
    printf("a != b = %d\n", a != b);
    return 0;
}

Booleans Are Integers

C has no separate boolean type by default. A comparison yields the int values 1 or 0. Any non-zero value counts as true in a condition.

#include <stdio.h>
int main(void) {
    int result = (10 > 2);
    printf("result = %d\n", result);
    return 0;
}

Logical AND

The && operator is true only when both operands are true.

#include <stdio.h>
int main(void) {
    int age = 25;
    if (age > 18 && age < 65)
        printf("Working age\n");
    return 0;
}

Logical OR

The || operator is true when at least one operand is true.

#include <stdio.h>
int main(void) {
    int day = 6;
    if (day == 6 || day == 7)
        printf("Weekend\n");
    return 0;
}

Logical NOT

The ! operator flips a truth value. !0 is 1 and !5 is 0.

#include <stdio.h>
int main(void) {
    int loggedIn = 0;
    if (!loggedIn)
        printf("Please log in\n");
    return 0;
}

Short-Circuit Evaluation

C uses short-circuiting: with && the right side is skipped if the left is false; with || it is skipped if the left is true.

#include <stdio.h>
int main(void) {
    int x = 0;
    if (x != 0 && 10 / x > 1)
        printf("safe\n");
    else
        printf("division skipped\n");
    return 0;
}

Combining Conditions

You can mix relational and logical operators to express rich conditions.

#include <stdio.h>
int main(void) {
    int score = 75;
    if (score >= 50 && score < 80)
        printf("Pass\n");
    return 0;
}

Negating Comparisons

! can wrap a whole comparison. !(a == b) is the same as a != b.

#include <stdio.h>
int main(void) {
    int a = 3, b = 5;
    printf("%d\n", !(a == b));
    return 0;
}

Chaining Is Not Math

Unlike math, 1 < x < 10 does not mean what you expect. It evaluates left to right as comparisons. Use x > 1 && x < 10 instead.

#include <stdio.h>
int main(void) {
    int x = 50;
    if (x > 1 && x < 10)
        printf("in range\n");
    else
        printf("out of range\n");
    return 0;
}

A Decision Example

Combine everything to classify a number.

#include <stdio.h>
int main(void) {
    int n = -4;
    if (n > 0)
        printf("positive\n");
    else if (n < 0)
        printf("negative\n");
    else
        printf("zero\n");
    return 0;
}

Quick Check

Test your understanding of relational and logical operators.

Recap: Relational and Logical

You learned how to compare and combine conditions.

  • Relational operators yield 1 or 0
  • == != test equality, never confuse == with =
  • && || ! combine truth values
  • Short-circuiting can skip the second operand
  • Chain conditions with explicit &&
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Relational and Logical” lesson free?

Yes — the full text of “Relational and Logical” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “Relational and Logical”?

Comparisons and &&, ||. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Relational and Logical” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic Operators
  2. Relational and Logical
  3. Assignment and Increment
  4. Operator Precedence
← Back to C Academy