Skip to content
StudyFame

if-else Statement in C

The if statement does something when a condition is true, but does nothing when it is false. Most decisions have two paths: pass or fail, even or odd, adult or minor.

The if-else statement handles both paths. In this lesson you will learn its syntax and flowchart, and write the two most common exam programs: even/odd and pass/fail.

What You Will Learn

  • The syntax and flowchart of if-else.
  • How exactly one of the two blocks runs.
  • Even or odd program.
  • Pass or fail program.
  • The dangling else rule.

if-else Statement in C Explained

Exam definition: The if-else statement executes the if block when the condition is true and the else block when it is false. Exactly one of the two blocks is always executed.

Memory trick: if-else is a fork in the road. You must take exactly one path: the left road (if) or the right road (else). You can never take both, and you can never take neither.

Syntax of if-else in C

if (condition)
{
    /* runs when condition is true */
}
else
{
    /* runs when condition is false */
}

The else has no condition of its own. It simply means "otherwise".

Flowchart of if-else

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

Example 1: Even or Odd Program in C Using if-else

#include <stdio.h>

int main(void)
{
    int n;

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

    if (n % 2 == 0)
    {
        printf("%d is even.\n", n);
    }
    else
    {
        printf("%d is odd.\n", n);
    }

    return 0;
}

Output

Enter a number: 27
27 is odd.

Code Explanation

  1. n % 2: the remainder when n is divided by 2. For 27 it is 1.
  2. n % 2 == 0: is the remainder 0? For 27, no, so the condition is false.
  3. Because it is false, the else block runs and prints "27 is odd."
  4. For 28, the remainder is 0, the condition is true, and the if block prints "28 is even."

How if-else Works

  1. C evaluates the condition once.
  2. If it is true, C runs the if block and then jumps over the else block.
  3. If it is false, C skips the if block and runs the else block.
  4. So exactly one block runs, never both.
  5. Dangling else rule: an else always belongs to the nearest unmatched if above it. Use braces to make the pairing clear.

Example 2: Pass or Fail Program in C

#include <stdio.h>

int main(void)
{
    int marks;

    printf("Enter your marks (0-100): ");
    scanf("%d", &marks);

    if (marks >= 40)
    {
        printf("Result: PASS\n");
        printf("Congratulations!\n");
    }
    else
    {
        printf("Result: FAIL\n");
        printf("You need %d more marks to pass.\n", 40 - marks);
    }

    return 0;
}

Output

Enter your marks (0-100): 72
Result: PASS
Congratulations!

With 35 marks, the else block runs and prints "Result: FAIL" and "You need 5 more marks to pass."

Important Points to Remember

  • if-else runs exactly one of the two blocks.
  • else has no condition.
  • Even check: n % 2 == 0.
  • An else pairs with the nearest unmatched if (dangling else).
  • Use braces for blocks with more than one statement.

Common Mistakes Beginners Make

MistakeWhat happensFix
else (marks < 40)Syntax error: else takes no conditionWrite just else, or use else if
if (x > 0); else ...Error: 'else' without a previous 'if'Remove the semicolon after if
Two statements in the if without braces before elseError: else without ifWrap the if block in { }
Using two separate ifs for opposite conditionsWorks, but checks twiceUse if-else

if-else Practice Questions

  1. What is the difference between if and if-else?
  2. Write the syntax of if-else.
  3. Can both the if block and the else block run? Why?
  4. What is the dangling else problem?
  5. What is the output of if (0) printf("A"); else printf("B");?
Show answers
  1. if does something only when true; if-else also does something else when false.
  2. if (condition) { ... } else { ... }
  3. No. The condition is either true or false, so exactly one block runs.
  4. When ifs are nested without braces, it can be unclear which if an else belongs to. Rule: it belongs to the nearest unmatched if.
  5. B

Coding Exercise

Read a person's age and print "Eligible to vote" if it is 18 or more, otherwise print how many years are left.

Show solution
#include <stdio.h>

int main(void)
{
    int age;

    printf("Enter age: ");
    scanf("%d", &age);

    if (age >= 18)
        printf("Eligible to vote\n");
    else
        printf("Not eligible. Wait %d more years.\n", 18 - age);

    return 0;
}
Enter age: 15
Not eligible. Wait 3 more years.

Quick Quiz: if-else in C (MCQ)

  1. In if-else, how many blocks run?

    A) 0   B) Exactly 1   C) Both   D) Depends

  2. Which is correct?

    A) else (x < 0)   B) else   C) else:   D) else; before the block

  3. Output of int x = 4; if (x % 2) printf("odd"); else printf("even");

    A) odd   B) even   C) 0   D) Error

  4. An else belongs to:

    A) The first if in the program   B) The nearest unmatched if   C) Any if   D) main()

  5. Which condition checks if n is even?

    A) n % 2 == 1   B) n / 2 == 0   C) n % 2 == 0   D) n == 2

Show answers
  1. B: exactly one.
  2. B: else
  3. B: even (4 % 2 is 0, false).
  4. B: the nearest unmatched if.
  5. C: n % 2 == 0

Quick Revision for Exams and Interviews

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

Must-remember facts

if-elseTrue → if block, false → else block
Blocks executedExactly one
else conditionNone (means "otherwise")
Even / oddn % 2 == 0 / n % 2 != 0
Dangling elseelse pairs with the nearest unmatched if

Frequently asked questions

What is the difference between if and if-else?
if handles only the true case; if-else handles both true and false cases.
Can an else exist without an if?
No. The compiler gives 'else' without a previous 'if'.
What is the dangling else problem?
Confusion about which if an else belongs to; C pairs it with the nearest unmatched if.
How do you check even or odd in C?
if (n % 2 == 0) even, else odd.

Remember

  • if-else = fork in the road: exactly one path.
  • else never has a condition.

Next Topic

Next lesson: else-if Ladder in C. You will learn how to check many conditions one after another, like a grade calculator.