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
n % 2: the remainder when n is divided by 2. For 27 it is 1.n % 2 == 0: is the remainder 0? For 27, no, so the condition is false.- Because it is false, the else block runs and prints "27 is odd."
- For 28, the remainder is 0, the condition is true, and the if block prints "28 is even."
How if-else Works
- C evaluates the condition once.
- If it is true, C runs the if block and then jumps over the else block.
- If it is false, C skips the if block and runs the else block.
- So exactly one block runs, never both.
- Dangling else rule: an
elsealways belongs to the nearest unmatchedifabove 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.
elsehas no condition.- Even check:
n % 2 == 0. - An
elsepairs with the nearest unmatched if (dangling else). - Use braces for blocks with more than one statement.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
else (marks < 40) | Syntax error: else takes no condition | Write 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 else | Error: else without if | Wrap the if block in { } |
| Using two separate ifs for opposite conditions | Works, but checks twice | Use if-else |
if-else Practice Questions
- What is the difference between if and if-else?
- Write the syntax of if-else.
- Can both the if block and the else block run? Why?
- What is the dangling else problem?
- What is the output of
if (0) printf("A"); else printf("B");?
Show answers
ifdoes something only when true;if-elsealso does something else when false.if (condition) { ... } else { ... }- No. The condition is either true or false, so exactly one block runs.
- When ifs are nested without braces, it can be unclear which if an else belongs to. Rule: it belongs to the nearest unmatched if.
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)
-
In if-else, how many blocks run?
A) 0 B) Exactly 1 C) Both D) Depends
-
Which is correct?
A)
else (x < 0)B)elseC)else:D)else;before the block -
Output of
int x = 4; if (x % 2) printf("odd"); else printf("even");A) odd B) even C) 0 D) Error
-
An else belongs to:
A) The first if in the program B) The nearest unmatched if C) Any if D) main()
-
Which condition checks if n is even?
A)
n % 2 == 1B)n / 2 == 0C)n % 2 == 0D)n == 2
Show answers
- B: exactly one.
- B:
else - B: even (4 % 2 is 0, false).
- B: the nearest unmatched if.
- 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-else | True → if block, false → else block |
|---|---|
| Blocks executed | Exactly one |
| else condition | None (means "otherwise") |
| Even / odd | n % 2 == 0 / n % 2 != 0 |
| Dangling else | else pairs with the nearest unmatched if |
Frequently asked questions
- What is the difference between if and if-else?
ifhandles only the true case;if-elsehandles 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.