HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to CVariablesData TypesOperatorsStringsArraysConditions (if / switch)Loops (for / while)FunctionsPointers
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
👉 Pointer
C›Conditions (if / switch)

Conditions (if / switch)

Making decisions with if-else, else if, and switch.

Simple enough for a 5-year-old to understand

if / else

Example code
1
#include <stdio.h>
2
int main() {
3
int score = 75;
4
if (score >= 50) {
5
printf("Passed\n");
6
} else {
7
printf("Failed\n");
8
}
9
return 0;
10
}
💡 Hint: In C, 0 = false, non-zero = true. A separate bool type is not required.

About this topic

In C, a conditional expression is evaluated as an integer: zero is false, any non-zero value is true. Since there is no separate boolean type requirement, any number, character, and pointer can be written as a condition.

This flexibility seems like a convenience, but it is one of C's most error-prone features. When you accidentally type an assignment instead of a comparison, the code compiles because assignment also produces a value, and that value counts as the condition.

The second trap is curly braces. C does not require braces for single-statement blocks; when a second line is added later, that line falls outside the condition. Since the indentation looks correct, the error is hard to spot visually.

What you will learn

  • the if, else if, and else structure
  • that zero is false and other values are considered true
  • why you should always write curly braces
  • the switch structure and the necessity of break

When to use?

In cases where a single variable is compared against many constant values, switch is both more readable and usually faster; the compiler can generate a jump table.

If range checking or complex logic is needed, an if chain is used; switch only works with integer and character constants.

Common mistakes

Wrong

#include <stdio.h>

int main() {
    int score = 50;
    if (score > 80)
        printf("Successful\n");
        printf("Congratulations\n");
    return 0;
}

Since there are no curly braces, only the FIRST line is bound to the condition. Even though the second line is indented, it is outside the condition and always runs. This error does not cause a compilation error; it only produces incorrect output and is hard to find visually.

Correct

#include <stdio.h>

int main() {
    int score = 50;
    if (score > 80) {
        printf("Successful\n");
        printf("Congratulations\n");
    }
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    int day = 2;
    switch (day) {
        case 1:
            printf("Monday\n");
        case 2:
            printf("Tuesday\n");
        case 3:
            printf("Wednesday\n");
    }
    return 0;
}

Because break is not written, the flow continues to FALL THROUGH downwards after the matching case: both Tuesday and Wednesday are printed to the screen. This is a deliberate design in C and is used in some cases, but most of the time it means a forgotten break.

Correct

#include <stdio.h>

int main() {
    int day = 2;
    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
    }
    return 0;
}

Wrong

#include <stdio.h>

int main() {
    int age = 25;
    if (18 <= age <= 65) {
        printf("Working age\n");
    }
    return 0;
}

Chained comparison does not work as expected in C. The comparison on the left is evaluated first and produces zero or one; then that result is compared with 65. Since one is always less than 65, the condition is always true. You need to combine the two conditions with and.

Correct

#include <stdio.h>

int main() {
    int age = 25;
    if (age >= 18 && age <= 65) {
        printf("Working age\n");
    }
    return 0;
}

Frequently asked questions

How do I catch when I write a single equals sign in a condition?

If you enable compiler warnings, GCC gives the warning "suggest parentheses around assignment used as truth value". Using the option that turns warnings into errors completely prevents this mistake. The habit of writing the constant on the left also works because assigning to a constant causes a compilation error.

Can I always use if instead of switch?

Yes, everything a switch does can be written with an if chain. The reverse is not true: switch cannot compare text, cannot do range checks, and only works with constants. When there are many constant values, switch is more readable.

When is the ternary operator used?

When choosing one of two values and assigning it to a variable. As long as it stays on a single line, it is readable; when nested, it becomes much harder to read than an if chain. When you feel like writing a nested ternary operator, using if is the right decision.

Summary

Zero is false, every value other than zero is true.

Always write curly braces even for single-line blocks.

If break is not written inside a switch, the execution flow continues downwards.

Chained comparison does not work; combine conditions with and.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code