Skip to content
AgileLogics
4 min read

Conditionals

Prerequisites: Reading user input

Syntax

if (age >= 18) {
    printf("Adult\n");
} else if (age >= 13) {
    printf("Teenager\n");
} else {
    printf("Child\n");
}

Example

int temperature = 15;
if (temperature > 30) {
    printf("Hot\n");
} else if (temperature > 15) {
    printf("Mild\n");
} else {
    printf("Cold\n");
}

Expected output

Cold

C has no true boolean type in its original form — any non-zero value is treated as "true" and 0 as "false" (C99 added a bool type via <stdbool.h> that's just a thin, more readable wrapper around this same rule).

Common mistakes

  • Writing if (x = 5) instead of if (x == 5) (this was flagged under Operators too — it's common enough to bear repeating).

Practice

Write a condition that prints "Pass" if a score is 50 or above, otherwise "Fail".

Show solution
int score = 42;
if (score >= 50) {
    printf("Pass\n");
} else {
    printf("Fail\n");
}