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

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
  • Non-zero is true, 0 is false
⭐ Read if you have time
  • Ternary cond ? a : b
  • Keep braces even for single-line bodies
  • Indentation of nested if

if Statement — Branch Based on a Condition

if (condition) {
  // runs when condition is true
}
Start
Condition true?
Yes
Run the body
No
Skip
Next statement

if-else / else if

int score = 75;
if (score >= 90) { printf("Excellent\n"); }
else if (score >= 70) { printf("Good\n"); }
else if (score >= 50) { printf("Pass\n"); }
else { printf("Fail\n"); }

Grade Demo

Grade
Good
Branch taken
else if (score >= 70)

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
Advertisement

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 10: Comparison & Logical Operators
Next lesson →
Lesson 12: Quiz (if)

Check Your Understanding

Check your understanding of this lesson!

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 if (x = 5) mean?

Test whether x equals 5
Assign 5 to x; always true
Compile error

= is assignment, so 5 is stored into x. The result of the assignment is 5 (non-zero = true), so the branch always runs. This is a classic bug — you meant ==.

Q3. How many elses can one if have?

Only one
Any number
Up to two

Each if can have only one else. Use else if for multi-way branching.

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

Recommended Books for This Lesson

Learning the concepts here + doing exercises in a book is very effective

πŸ“˜
Suffer Through C
MMGames
A classic introductory book for beginners. Careful explanations cover the fundamentals.
View on Amazon
πŸ“—
New Clear C Programming: Introduction
Bohyoh Shibata
Plenty of diagrams and practice problems. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language (2nd Edition)
B.W. Kernighan, D.M. Ritchie
Known as K&R. The original C text. Perfect for the next step after basics.
View on Amazon

* These are affiliate links. Your purchases help support this site.