An if statement runs a block of code only when a condition is true. If the condition is false, that block is skipped. In C, 0 is false and any non-zero value is true.
Remember
if (condition) {
/* runs only when condition is true */
}
0 → false
non-zero → true
== compare = assign (do not mix them up)
use { } braces even for one statement
Simulate simple if checks in the browser. Same ideas as the C examples below (compile those with gcc).
Browser simulation of an if condition (same idea as the C examples below).
Result
n = 5
if (n > 0) → true
→ print "positive"
(body runs)
Trace
Worked Walkthrough — Is It Positive?
Trace if (n > 0) with two values. Same pattern as Example 1.
Trace
Case A: n = 5
check n > 0? true → run body → print "positive"
Case B: n = -2
check n > 0? false → skip body → nothing printed
if alone has no "else" path — false means skip.
Code
C Examples
Compile every example with gcc file.c -std=c11 -o out. Next: if-else when you need a false branch.
Example 1 — Print Only If Positive
The body runs only when n > 0. If n is zero or negative, nothing is printed.
C
#include <stdio.h>
int main(void) {
int n = 5;
if (n > 0) {
printf("positive\n");
}
return 0;
}
Output
positive
How It Works
Set a value.n = 5 is what we will test.
Evaluate the condition.n > 0 is true.
Run the body.printf prints positive.
If it were false. With n = -1, the body would be skipped entirely.
Example 2 — Equality and Even Check
Use == for equality and % for remainder. Never use = inside the condition by accident.
C
#include <stdio.h>
int main(void) {
int n = 8;
if (n == 0) {
printf("zero\n");
}
if (n % 2 == 0) {
printf("even\n");
}
return 0;
}
Output
even
How It Works
First check.n == 0 is false for 8 — that if is skipped.
Remainder.n % 2 is 0 when n is even.
Second check.n % 2 == 0 is true → print even.
Remember.== compares; = assigns.
Example 3 — Combine Conditions with &&
Both sides of && must be true. For an alternative path when false, use if-else.
C
#include <stdio.h>
int main(void) {
int age = 20;
int has_id = 1; /* 1 = true, 0 = false */
if (age >= 18 && has_id) {
printf("allowed\n");
}
return 0;
}
Output
allowed
How It Works
Left side.age >= 18 is true for 20.
Right side.has_id is 1 (non-zero → true).
Combine.&& requires both true → body runs.
Short-circuit. If the left side were false, C would skip evaluating the right side.
Edge Cases & Pitfalls
Check these before relying on if in real projects.
assign
= instead of ==
if (x = 5) assigns 5 and is always true. Enable warnings (-Wall) and prefer ==.
braces
Missing braces
Without { }, only the next statement is guarded. A second line you add later always runs — a classic bug.
float
Comparing floats with ==
Floating-point rounding can make exact equality fail. Prefer a small epsilon range, or compare integers when possible.
empty
Empty body by mistake
if (ok); with a trailing semicolon is an empty if—the next line always runs.
Analysis
Performance Notes
Approach
Cost
Best for
Simple if
Negligible (branch)
Everyday decisions
&& / || short-circuit
May skip right side
Cheap tests first; avoid side effects on the right
An if itself is cheap. Clarity matters more than micro-optimizing branches. Put the most likely or cheapest test first when combining with && / ||.
Remember
Key Takeaways
Role: run code only when a condition is true.
Truth:0 is false; anything else is true.
Compare: use ==, never assign with = in a test.
Style: always use braces; add else when you need a false path.
One line:if runs the body only when the condition is true—compare with ==, brace the body.
Frequently Asked Questions
An if statement runs a block of code only when a condition is true. Syntax: if (condition) { ... }. If the condition is false, the block is skipped and execution continues after it.
Conditions are integer expressions. Zero is false; any non-zero value is true. Comparisons like x > 0 yield 1 for true and 0 for false. C99 adds _Bool / stdbool.h for clearer booleans.
= is assignment (stores a value). == is comparison (tests equality). Writing if (x = 5) assigns 5 to x and is always true—almost always a bug. Use if (x == 5) to compare.
C allows if (cond) single_statement; without braces, but always use { } in real code. Braces prevent bugs when you add a second line later and make nesting easier to read.
Yes. An if inside another if runs only when both outer and inner conditions are true. For multi-way choices, prefer else-if or switch when they fit better.
if alone runs code only when the condition is true and does nothing otherwise. if-else adds an alternative block when the condition is false.