C Notes
Master the if-else statement in C with syntax, flowchart, else-if ladder, examples, and interview questions. Complete guide for beginners.
The if-else statement extends the basic if statement by providing an alternative path of execution when the condition is false. Instead of just skipping code, your program can choose between two different actions — like a fork in the road where you must go either left or right.
This is one of the most commonly used control structures in any C program. Whether you're building a login system, a calculator, or a game, the if-else statement helps you handle both the "yes" and "no" scenarios gracefully.
Syntax of if-else Statement
if (condition) {
// executed when condition is true
} else {
// executed when condition is false
}| Part | Purpose |
|---|---|
if (condition) | Evaluates the expression |
First block {} | Runs when condition is non-zero (true) |
else | Keyword introducing the alternative |
Second block {} | Runs when condition is zero (false) |
Flowchart of if-else Statement
Basic Example
#include <stdio.h>
int main() {
int number = -7;
if (number >= 0) {
printf("%d is positive or zero.\n", number);
} else {
printf("%d is negative.\n", number);
}
return 0;
}-7 is negative.
Practical Examples
Example 1: Even or Odd Checker
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}
return 0;
}Enter an integer: 13 13 is an odd number.
Example 2: Pass or Fail
#include <stdio.h>
int main() {
float marks;
printf("Enter your marks (out of 100): ");
scanf("%f", &marks);
if (marks >= 40.0) {
printf("Congratulations! You passed.\n");
printf("Your marks: %.1f\n", marks);
} else {
printf("Sorry, you failed.\n");
printf("You need %.1f more marks to pass.\n", 40.0 - marks);
}
return 0;
}Enter your marks (out of 100): 35.5 Sorry, you failed. You need 4.5 more marks to pass.
Example 3: Leap Year Check
Enter a year: 2024 2024 is a leap year.
The else-if Ladder
When you need to test multiple conditions in sequence, you use the else-if ladder. It checks conditions one by one from top to bottom, and as soon as one condition is true, it executes that block and skips the rest.
Syntax
if (condition1) {
// executes if condition1 is true
} else if (condition2) {
// executes if condition2 is true
} else if (condition3) {
// executes if condition3 is true
} else {
// executes if none of the above are true
}Flowchart of else-if Ladder
Example: Grade Calculator
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A+ (Outstanding)\n");
} else if (marks >= 80) {
printf("Grade: A (Excellent)\n");
} else if (marks >= 70) {
printf("Grade: B (Good)\n");
} else if (marks >= 60) {
printf("Grade: C (Average)\n");
} else if (marks >= 50) {
printf("Grade: D (Below Average)\n");
} else {
printf("Grade: F (Fail)\n");
}
return 0;
}Enter marks (0-100): 73 Grade: B (Good)
Example: Calculator Using else-if
#include <stdio.h>
int main() {
float num1, num2, result;
char operator;
printf("Enter expression (e.g., 5 + 3): ");
scanf("%f %c %f", &num1, &operator, &num2);
if (operator == '+') {
result = num1 + num2;
printf("%.2f + %.2f = %.2f\n", num1, num2, result);
} else if (operator == '-') {
result = num1 - num2;
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
} else if (operator == '*') {
result = num1 * num2;
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
} else if (operator == '/') {
if (num2 != 0) {
result = num1 / num2;
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero!\n");
}
} else {
printf("Invalid operator '%c'\n", operator);
}
return 0;
}Enter expression (e.g., 5 + 3): 10 / 2 10.00 / 2.00 = 5.00
Ternary Operator: Shorthand for if-else
C provides a compact alternative to simple if-else statements called the ternary operator (?:):
#include <stdio.h>
int main() {
int a = 10, b = 20;
// Using if-else
int max;
if (a > b) {
max = a;
} else {
max = b;
}
// Same thing using ternary operator
int max2 = (a > b) ? a : b;
printf("Maximum: %d\n", max2);
return 0;
}Maximum: 20
Common Mistakes
Mistake 1: else Without Matching if
// WRONG - else must immediately follow an if block
if (x > 0) {
printf("Positive\n");
}
printf("Hello\n"); // This separates if from else!
else { // Compile error!
printf("Not positive\n");
}Mistake 2: Multiple else Blocks
// WRONG - Only one else per if
if (x > 0) {
printf("Positive\n");
} else {
printf("Zero or negative\n");
} else { // Error!
printf("Something else\n");
}Mistake 3: Forgetting the Order in else-if
// WRONG - First condition always catches everything >= 50
if (marks >= 50) {
printf("Grade D\n");
} else if (marks >= 60) {
printf("Grade C\n"); // Never reached!
} else if (marks >= 70) {
printf("Grade B\n"); // Never reached!
}
// CORRECT - Check from highest to lowest
if (marks >= 70) {
printf("Grade B\n");
} else if (marks >= 60) {
printf("Grade C\n");
} else if (marks >= 50) {
printf("Grade D\n");
}if-else vs Ternary Operator
| Feature | if-else | Ternary Operator |
|---|---|---|
| Readability | Better for complex logic | Better for simple assignments |
| Multiple statements | Supports multiple | Only single expressions |
| Nesting | Clear with braces | Gets confusing quickly |
| Usage | General purpose | Inline expressions |
Best Practices
- Always handle the else case — don't leave edge cases unhandled
- Order conditions from most specific to least in else-if ladders
- Keep conditions simple and readable — use helper variables for complex logic
- Use the ternary operator sparingly — only for simple, clear assignments
- Validate input before processing it in if-else chains
Interview Questions
Q1: Can we have an else without an if in C?
No. The else keyword must always be paired with a preceding if statement. An orphan else will cause a compilation error.
Q2: What is the maximum number of else-if blocks we can have?
There is no language-defined limit on the number of else-if blocks. However, practically, if you have many conditions, a switch statement or lookup table might be more appropriate and readable.
Q3: Is the else block mandatory in an if-else statement?
No. The else block is optional. A standalone if statement without else is perfectly valid. The else simply provides an alternative path.
Q4: What is the difference between multiple if statements and an else-if ladder?
With multiple separate if statements, every condition is checked regardless of previous results. In an else-if ladder, once a condition is true, remaining conditions are skipped. This affects both performance and logic.
Q5: Can we write an else-if ladder without a final else?
Yes. The final else is optional. However, including it as a default case is good defensive programming practice — it handles unexpected scenarios.
Summary
The if-else statement gives your program the ability to choose between two paths of execution. The else-if ladder extends this to multiple choices checked sequentially. Remember that only one block in an else-if chain ever executes — the first one whose condition is true. For simple binary choices, the ternary operator provides a compact alternative. Always order your conditions carefully in else-if ladders, checking from the most restrictive to the least restrictive condition.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for if-else Statement in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, else, statement, if-else statement in c programming
Related C Programming Topics