The if statement is how C programs make decisions. When a condition is true, a block of code runs; when false, that block is skipped. This tutorial covers syntax, comparisons, logical operators, nesting, and five practical examples every beginner should try.
01
Syntax
if (cond).
02
True/false
Non-zero.
03
==
Compare.
04
&& || !
Logic ops.
05
Nested
if in if.
06
Braces
{ } always.
Fundamentals
Definition and Usage
An if statement evaluates a condition in parentheses. If the result is non-zero (true), the statement or block inside runs. If the result is zero (false), C skips that block and continues with the next line after it.
Without if, every program would run the same steps every time. Conditions let you react to user input, compare values, detect errors, and branch into different logic paths. You can chain alternatives with else and else if in follow-up tutorials.
💡
Beginner Tip
Use == to compare values inside if, not =. Writing if (x = 5) assigns 5 to x and is treated as true—a common beginner mistake.
Foundation
📝 Syntax
Simplest form of the C if statement:
C
if (condition) {
// runs when condition is true (non-zero)
}
Common condition operators
== equal, != not equal
<<=>>= comparisons
&& AND (both true), || OR (either true), ! NOT
Related forms (next tutorials)
C
if (condition) {
/* true branch */
} else {
/* false branch — see if-else tutorial */
}
if (a) { }
else if (b) { } /* see else-if tutorial */
else { }
Compile
C
gcc program.c -std=c11 -o program
Cheat Sheet
⚡ Quick Reference
Pattern
Example
Positive test
if (n > 0) { ... }
Equality
if (ch == 'A') { ... }
Even number
if (n % 2 == 0) { ... }
AND two tests
if (n > 0 && n % 2 == 0) { ... }
Range
if (age >= 18 && age <= 65) { ... }
True
non-zero
Runs body
False
0
Skips body
Compare
== !=
Not =
Braces
{ }
Always use
Hands-On
Examples Gallery
Compile with gcc example.c -std=c11 -o example. Each program demonstrates a core if pattern.
📚 Getting Started
Simple conditions and comparisons.
Example 1 — Check If a Number Is Positive
Print a message only when num > 0.
C
#include <stdio.h>
int main(void) {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
}
return 0;
}
📤 Output:
The number is positive.
How It Works
10 > 0 is true (1), so the printf runs. If num were negative, nothing would print and the program would go straight to return 0.
Example 2 — Check If a Number Is Even
Use the modulo operator % to test divisibility by 2.
C
#include <stdio.h>
int main(void) {
int num = 14;
if (num % 2 == 0) {
printf("%d is even.\n", num);
}
return 0;
}
📤 Output:
14 is even.
How It Works
num % 2 is the remainder after dividing by 2. A remainder of 0 means even. Use == to compare the remainder to zero.
📈 Practical Patterns
Logical operators, nesting, and real-world checks.
Example 3 — Positive AND Even
Combine conditions with && (logical AND).
C
#include <stdio.h>
int main(void) {
int num = 20;
if (num > 0 && num % 2 == 0) {
printf("%d is positive and even.\n", num);
}
return 0;
}
📤 Output:
20 is positive and even.
How It Works
Both sides of && must be true. -4 is even but not positive, so the message would not print for num = -4.
Example 4 — Nested if Statements
Check positivity first, then even or odd inside.
C
#include <stdio.h>
int main(void) {
int num = 15;
if (num > 0) {
if (num % 2 == 0) {
printf("Positive and even.\n");
} else {
printf("Positive and odd.\n");
}
}
return 0;
}
📤 Output:
Positive and odd.
How It Works
The inner if runs only when the outer num > 0 is true. The inner else handles the odd case; see the dedicated if-else tutorial for more.
Example 5 — Working-Age Range Check
Test whether a value falls between two bounds.
C
#include <stdio.h>
int main(void) {
int age = 25;
if (age >= 18 && age <= 65) {
printf("Age %d is in the typical working range.\n", age);
}
if (age < 13) {
printf("Child ticket price applies.\n");
}
return 0;
}
📤 Output:
Age 25 is in the typical working range.
How It Works
Multiple independent if statements can each test different rules. Only the conditions that are true run their blocks.
Applications
🚀 Common Use Cases
Input validation — reject invalid numbers or empty input.
Threshold checks — pass/fail, minimum balance, age limits.
Error handling — run cleanup only when a pointer is NULL.
Feature flags — enable debug output when a variable is set.
Bounds checking — guard array indices before access.
Character tests — react to specific keys or tokens.
🧠 How the if Statement Works
1
Evaluate condition
Compute expression inside ( ).
Test
2
Non-zero?
True → run body. Zero → skip body.
Branch
3
Run or skip
Execute block statements or jump past them.
Action
=
➜
Continue program
Next statement after the if block runs.
Important
📝 Notes
Zero is false; any non-zero value is true (including negative numbers).
if (x = 5) assigns—use if (x == 5) to compare.
Without braces, only the next statement belongs to the if.
if (condition); with a lone semicolon is an empty body—a common bug.
&& and || short-circuit: right side may not evaluate.
For mutually exclusive cases, use else if instead of many separate ifs.
Performance
⚡ Optimization
Put the most likely or cheapest condition first in && chains so short-circuit evaluation skips expensive tests. For readability, extract complex conditions into bool variables (C99 stdbool.h) with clear names like is_valid_age instead of long inline expressions.
Wrap Up
Conclusion
The C if statement is the foundation of decision-making. Master comparisons, logical operators, and braces, then move on to if-else and else-if for two-way and multi-way branches.
Practice the examples until reading if (n > 0 && n % 2 == 0) feels natural—you will use it in almost every C program.
Assume if (ptr) replaces proper NULL checks everywhere
Compare float with == for exact equality
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about the C if statement
Your foundation for conditional logic in C.
5
Core concepts
📝01
Non-zero
Is true.
Basics
🔢02
==
Compare.
Syntax
🔗03
&& ||
Combine.
Logic
🕸04
Nested
if in if.
Structure
{ }05
Braces
Always.
Safety
❓ 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 the closing brace.
C has no boolean type in older standards—conditions are integer expressions. Zero is false; any non-zero value is true. Comparisons like x > 0 return 1 for true and 0 for false.
= 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. See the nested if example below; for deep chains consider else-if or logical operators.
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. See the if-else tutorial next in the sidebar.
Did you know?
In C, if (ptr) is idiomatic shorthand for if (ptr != NULL)—non-null pointers are true. This pattern appears throughout real C codebases for quick null checks before dereferencing.