C if Statement

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is an if Statement?

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
UsePrefer
Run code only if trueif (this page)
True path + false pathif-else
Several related choiceselse-if / switch

How to Use if in C

Write a condition, put the optional work in braces, then compile.

  1. Decide what must be true (e.g. n > 0 or x == y).
  2. Write if (condition) { ... } with braces around the body.
  3. Use ==, !=, <, >, &&, ||, ! as needed.
  4. Compile with gcc file.c -std=c11 -o out.
PieceRequired?Role
ConditionYesExpression tested before the body
Body { }Strongly recommendedCode that runs when true
elseNoOptional false path — see if-else

Pseudocode

Pseudocode
read or set values used in the test

if condition is true:
    do the work
// otherwise skip the block

continue with the rest of the program

Cheat Sheet

Keep these facts handy while writing conditions.

TopicDetail
Syntaxif (cond) { ... }
Truth0 false; non-zero true
Compare== != < > <= >=
Combine&& and, || or, ! not
Assign trapif (x = 1) assigns — use ==
BracesAlways use { } for clarity
Next stepif-else for a false branch

Live Preview

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)

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.

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

How It Works

  1. Set a value. n = 5 is what we will test.
  2. Evaluate the condition. n > 0 is true.
  3. Run the body. printf prints positive.
  4. 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;
}

How It Works

  1. First check. n == 0 is false for 8 — that if is skipped.
  2. Remainder. n % 2 is 0 when n is even.
  3. Second check. n % 2 == 0 is true → print even.
  4. 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;
}

How It Works

  1. Left side. age >= 18 is true for 20.
  2. Right side. has_id is 1 (non-zero → true).
  3. Combine. && requires both true → body runs.
  4. 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.

Performance Notes

ApproachCostBest for
Simple ifNegligible (branch)Everyday decisions
&& / || short-circuitMay skip right sideCheap tests first; avoid side effects on the right
Deep nested ifHarder to readPrefer else-if / switch when clearer

An if itself is cheap. Clarity matters more than micro-optimizing branches. Put the most likely or cheapest test first when combining with && / ||.

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.

Next: if-else Statement

Learn how to run a different block when the condition is false.

if-else tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful