πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Conditional Branching (if Statement)

How to write if statements in C, with flowcharts for visual understanding.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • if (cond) { ... } basic form
  • else handles the false case
  • else if for three or more branches β€” strictest first
  • Non-zero is true, 0 is false
⭐ Read if you have time
  • else if for three or more cases
  • Keep braces even for single-line bodies
  • How else binds in nested if statements

if Statement — Branch Based on a Condition

C programs run top to bottom by default, but often you only want a block to run when some condition is true. That's exactly what if does.
Think of it as the English word "if": "if the score is greater than 59, print Pass," or "if the balance is below 1, warn the user."
if (condition) {
  // runs when condition is true
}
Start
Condition true?
Yes
Run the body
No
Skip
Next statement

A first concrete example

int score = 75;
if (score > 59) {
    printf("Pass\n");
}
When score > 59 is true, the body inside { } runs. If score is 50, the condition is false and the body is skipped entirely.
This page's approach: we'll start by using only < and > to grasp the basic flow "true β†’ enter, false β†’ skip." Operators like ==, <=, >=, &&, and || are covered in the next lesson, "Comparison & Logical Operators."

Truthiness in C — anything non-zero is true

Before C99, C had no dedicated bool type (C99 added stdbool.h), so truth values are just integers.
true
anything non-zero
1, -1, 100, 3.14 ...
false
only 0
if (1)       // always true - body always runs
if (0)       // always false - body never runs
if (score)   // true when score is non-zero

Why braces { } matter

Braces { } group multiple statements into the if body. For a single-statement body, braces are technically optional β€” but beginners should always write them anyway.
βœ… With braces (safe)
if (x > 0) {
    printf("positive\n");
    count++;
}
❌ Without braces, two-line "body" becomes a bug
if (x > 0)
    printf("positive\n");
    count++;  // NOT inside if - always runs
⚠️ Indentation lies: even though count++; looks like it's inside the if, without braces only the very next statement counts as the body.

else — what runs when the condition is false

else is the "otherwise" clause. When the if condition is false, the else body runs instead.
In plain English: "if X, then do A; otherwise do B."
int score = 45;
if (score > 59) {
    printf("Pass\n");
} else {
    printf("Fail\n");
}
Start
score > 59 ?
Yes
print "Pass"
No
print "Fail"
Next statement
Key point: exactly one of the two bodies runs — the if body when true, the else body when false. Never both, and never neither.

else is optional

else isn't required. If you only care about the "true" case, just leave it off.
With else (two-way)
if (age > 17) {
    printf("adult\n");
} else {
    printf("minor\n");
}
Without else (truthy only)
if (errors > 0) {
    printf("warning\n");
}
// do nothing on success

else if — three or more branches

if + else covers two outcomes. For three or more cases, chain else if. It's really just shorthand for "put another if inside the else."
int score = 75;
if (score > 89) { printf("A\n"); }
else if (score > 69) { printf("B\n"); }
else if (score > 49) { printf("C\n"); }
else { printf("D\n"); }
Evaluated top to bottom: only the first branch whose condition is true runs; everything after it is skipped.
⚠️ Order matters: if you put score > 49 first, scores of 90 and 70 also match there, so they all stop at C. Always put the strictest (narrowest) condition first.
The trailing else catches "none of the above." Omit it and nothing happens when no case matches β€” a silent no-op. Writing it explicitly is safer.

Grade Demo

Grade
B
Branch taken
else if (score > 69)

Common if mistakes

The if statement has a short syntax but a few easy-to-misread spots. Before getting into specific comparison operators, let's nail down the structural traps.
Trap 1Stray ; right after the condition
if (score > 59);
{
    printf("Pass\n");
}
The ; right after the condition ends the if. The { ... } that follows is just a plain block, so it always runs regardless of score.
Trap 2Misreading which if the else attaches to
if (a > 0)
    if (b > 0)
        printf("both positive\n");
else
    printf("b is not positive\n");
In C, else binds to the nearest if. To avoid confusion, always use { } when nesting.
Trap 3Writing conditions from loose to strict
if (score > 49) {
    printf("C\n");
} else if (score > 89) {
    printf("A\n");
}
Even with score 95, the first score > 49 is true, so it never reaches A. Remember: else if chains are evaluated top-down.
Trap 4Writing a range like math notation
int x = 100;
if (2 < x < 5) {
    printf("in range\n");
}
In math this reads "greater than 2 and less than 5", but C doesn't parse it that way. It evaluates 2 < x first, gets 0 or 1, then compares that with < 5 — so even x = 100 comes out as "true". The proper way to write a range condition is covered in the next lesson, "Comparison & Logical Operators".

Step Execution — if Statement

if_demo.c

Variable State

NameTypeValue

Condition Evaluation

β€”

Standard Output

 

Try It Yourself — if Statement

my_if.c
Output
Press "Run" to execute...
πŸ’‘ Try these ideas too

Related Lessons

Operators
Comparison & Logical Operators
C comparison operators (==, !=, <, >) and logical operators (&&, ||, !).
Conditionals
else if / switch
How to use else if and switch in C for multi-way branching.
Conditionals
if Statement Quiz
Quiz to check your understanding of C if statements.
← Previous lesson
Lesson 11: Printf Debugging
Next lesson →
Lesson 13: Comparison & Logical Operators

Review Quiz

Test what you learned. To match the lesson, every problem here uses only < and >.

Q1. What happens when the condition of an if statement is 0?

The block is executed
The block is not executed
It causes an error

In C, 0 is false and anything non-zero is true. When the condition is 0, the if block is skipped.

Q2. What does this code print?

int x = 5;
if (x > 0) {
    printf("A\n");
} else {
    printf("B\n");
}
A
B
Nothing

5 > 0 is true, so the if-branch prints A.

Q3. What does this code print?

int x = -2;
if (x > 0) {
    printf("positive\n");
} else {
    printf("not positive\n");
}
positive
not positive
Compile error

-2 > 0 is false, so the else branch runs.

Q4. What does this code print?

int score = 75;
if (score > 89) {
    printf("A\n");
} else if (score > 69) {
    printf("B\n");
} else {
    printf("C\n");
}
A
B
C

75 isn't greater than 89, but it is greater than 69, so B prints.

Q5. What happens when conditions are written from loose to strict?

int score = 95;
if (score > 49) {
    printf("C\n");
} else if (score > 89) {
    printf("A\n");
}
C
A
Both A and C

95 already satisfies the first score > 49, so C prints and the else if is never evaluated. Lesson: write the strictest condition first.

Q6. What does this code print?

int score = 40;
if (score > 59);
{
    printf("Pass\n");
}
Nothing
Pass
Compile error

The stray ; after if (score > 59) ends the if right there. The block that follows has nothing to do with the if, so it always runs.

Q7. With a=5, b=-3, what does this print?

if (a > 0)
    if (b > 0)
        printf("both positive\n");
else
    printf("b is not positive\n");
both positive
b is not positive
Nothing

else binds to the nearest if. The outer a > 0 is true and the inner b > 0 is false, so the else runs.

Q8. What's the safe way to put multiple lines inside an if?

Just align the indentation
Wrap them with { }
Add 2 spaces at the start of each line

In C, blocks are determined by { }, not indentation. To put multiple lines inside an if, always wrap them.

Share this article
Share on X (Twitter) Share on Facebook Share on LINE Hatena Bookmark