Skip to content
StudyFame

if Statement in C

So far, every program ran all its lines from top to bottom. Real programs need to make decisions: show "Access granted" only if the password is right, give a discount only if the bill is above ₹1000.

The if statement is the simplest way to make a decision in C. In this lesson you will learn its syntax, flowchart and how C checks the condition.

What You Will Learn

  • What decision making (control flow) means.
  • The syntax and flowchart of the if statement.
  • How C decides if a condition is true.
  • When curly braces { } are needed.
  • The semicolon mistake that silently breaks an if.

if Statement in C Explained

Exam definition: The if statement is a decision-making statement that executes a block of code only when a given condition is true. If the condition is false, the block is skipped.

  • The condition goes inside brackets ( ).
  • In C, any non-zero value is true and 0 is false.
  • The if statement is part of control flow: statements that change the normal top-to-bottom order.

Memory trick: if is a gatekeeper. If your condition is true, the gate opens and you go inside the block. If false, you walk past the gate and continue.

Syntax of if Statement in C

if (condition)
{
    /* statements run only when condition is true */
}
/* next statement always runs */

If the block has only one statement, the braces are optional:

if (marks >= 40)
    printf("Pass\n");

Flowchart of the if statement

        [ Start ]
            |
     < condition true? >
        /         \
      Yes          No
       |            |
 [ if block ]       |
       |            |
        \          /
     [ next statement ]

Example 1: Check if a Number is Positive

#include <stdio.h>

int main(void)
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if (n > 0)
    {
        printf("%d is positive.\n", n);
    }

    printf("Program finished.\n");
    return 0;
}

Output

Enter a number: 15
15 is positive.
Program finished.

If you enter -4 instead, the output is only Program finished., because the if block is skipped.

Code Explanation

  1. scanf("%d", &n);: reads the number (15).
  2. if (n > 0): checks the condition. 15 > 0 is true (1).
  3. Because it is true, the statement inside { } runs and prints "15 is positive."
  4. printf("Program finished.\n"); is outside the if, so it runs every time.

How the if Statement Works

  1. C evaluates the expression inside the brackets.
  2. If the result is non-zero (true), it runs the statement or block right after the if.
  3. If the result is 0 (false), it jumps over that block.
  4. Either way, the program continues with the next statement after the block.
  5. Without braces, only the first statement after if belongs to it. Lines after that always run.

Example 2: The Semicolon Bug

#include <stdio.h>

int main(void)
{
    int age = 15;

    if (age >= 18);                 /* BUG: semicolon ends the if */
        printf("Can vote (wrong!)\n");

    if (age >= 18)                  /* correct */
        printf("Can vote\n");

    printf("Age checked.\n");
    return 0;
}

Output

Can vote (wrong!)
Age checked.

A semicolon right after if (...) makes an empty statement the body of the if. The next printf is no longer part of the if, so it always runs, even for a 15-year-old. The correct if (without ;) prints nothing because 15 < 18.

Important Points to Remember

  • if runs its block only when the condition is true.
  • Non-zero = true, 0 = false.
  • No semicolon after if (condition).
  • Without braces, only one statement belongs to the if.
  • Always use braces when the block may grow; it prevents bugs.

Common Mistakes Beginners Make

MistakeWhat happensFix
if (x > 0);The block always runsRemove the semicolon
if (x = 5)Assigns 5; always trueif (x == 5)
Two statements without bracesOnly the first is inside the ifUse { }
if x > 0 (no brackets)Syntax errorif (x > 0)
IF (x > 0)Error: C is case-sensitiveLowercase if

if Statement Practice Questions

  1. What is an if statement? Write its syntax.
  2. What happens if the condition of an if is false?
  3. What is the output of if (0) printf("A"); printf("B");?
  4. Why is if (x > 5); a mistake?
  5. Is if (-3) true or false?
Show answers
  1. A decision statement that runs a block only when the condition is true. if (condition) { statements }
  2. The block is skipped, and the program continues with the next statement.
  3. B (only the first statement belongs to the if, and 0 is false).
  4. The semicolon becomes the empty body of the if, so the following statement always runs.
  5. True (any non-zero value is true).

Coding Exercise

Read a shopping bill amount. If it is more than ₹1000, print "You get a 10% discount!" and the discounted amount. Always print "Thank you for shopping."

Show solution
#include <stdio.h>

int main(void)
{
    float bill;

    printf("Enter bill amount: ");
    scanf("%f", &bill);

    if (bill > 1000)
    {
        printf("You get a 10%% discount!\n");
        printf("Pay: %.2f\n", bill * 0.9f);
    }

    printf("Thank you for shopping.\n");
    return 0;
}
Enter bill amount: 1500
You get a 10% discount!
Pay: 1350.00
Thank you for shopping.

Quick Quiz: if Statement in C (MCQ)

  1. The if block runs when the condition is:

    A) 0   B) Non-zero   C) Negative only   D) Always

  2. Output of if (5 > 10) printf("Yes"); printf("Done");

    A) YesDone   B) Yes   C) Done   D) Nothing

  3. Which is correct?

    A) if x > 5   B) if (x > 5)   C) if [x > 5]   D) if {x > 5}

  4. Without braces, how many statements belong to an if?

    A) 0   B) 1   C) 2   D) All until the end

  5. if (x = 0) is:

    A) Always true   B) Always false   C) A comparison   D) An error

Show answers
  1. B: non-zero.
  2. C: Done.
  3. B: if (x > 5)
  4. B: 1.
  5. B: always false (it assigns 0).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

if statementRuns a block only when the condition is true
Syntaxif (condition) { statements }
True / falseNon-zero / 0
No bracesOnly 1 statement belongs to the if
if (x);Empty body bug
TypeDecision-making (selection) statement

Frequently asked questions

What is the use of the if statement?
To run code only when a condition is true, so the program can make decisions.
Are braces compulsory in an if statement?
No, only for more than one statement, but using them always is safer.
What happens with a semicolon after if?
The semicolon is the empty body, so the next statement always runs.
What values are true in C?
Any non-zero value, including negative numbers.

Remember

  • if = gatekeeper: true → enter the block, false → walk past.
  • No ; after if (...).

Next Topic

Next lesson: if-else Statement in C. You will learn how to do one thing when the condition is true and another when it is false.