C if Statement

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
Decision making

What You’ll Learn

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.

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.

📝 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

⚡ Quick Reference

PatternExample
Positive testif (n > 0) { ... }
Equalityif (ch == 'A') { ... }
Even numberif (n % 2 == 0) { ... }
AND two testsif (n > 0 && n % 2 == 0) { ... }
Rangeif (age >= 18 && age <= 65) { ... }
True
non-zero

Runs body

False
0

Skips body

Compare
==  !=

Not =

Braces
{ }

Always use

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;
}

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;
}

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;
}

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;
}

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;
}

How It Works

Multiple independent if statements can each test different rules. Only the conditions that are true run their blocks.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Always use braces { } around if bodies
  • Use == for equality comparisons
  • Name complex conditions with clear variables
  • Indent nested blocks consistently
  • Handle the false case with else when needed

❌ Don’t

  • Confuse = (assign) with == (compare)
  • Put a semicolon right after if (cond)
  • Nest many levels without refactoring
  • Assume if (ptr) replaces proper NULL checks everywhere
  • Compare float with == for exact equality

Key Takeaways

Knowledge Unlocked

Five things to remember about the C if statement

Your foundation for conditional logic in C.

5
Core concepts
🔢 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.

Continue to if-else

Learn what happens when the condition is false—the natural next step after if.

if-else tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful