Nearby lessons

39 of 124

C - if...else if Ladder

if...else if Ladder is one of the foundational topics in C programming. This lesson explains The if-else-if Ladder with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

The if-else-if Ladder

When there are more than two choices, chain else if blocks. Only the first true condition's block runs; the rest are skipped.

In simple words: the ladder checks from top to bottom and stops at the first true condition — the rest of the ladder is ignored. The final else catches the case where nothing above was true.
Example01
CCode Cell
1if (condition1) {
2 // block 1
3} else if (condition2) {
4 // block 2
5} else if (condition3) {
6 // block 3
7} else {
8 // default - when none are true
9}

The if-else-if Ladder

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int marks;
6 printf("Enter marks: ");
7 scanf("%d", &marks);
8 
9 if (marks >= 90) printf("Grade A\n");
10 else if (marks >= 75) printf("Grade B\n");
11 else if (marks >= 60) printf("Grade C\n");
12 else if (marks >= 40) printf("Grade D\n");
13 else printf("Fail\n");
14}
Output
Enter marks: 80 Grade B
📝 Key Takeaways
  • if runs a block when the condition is true; else covers the false case.
  • if-else-if ladder chooses the first true condition.
  • Nested if = an if inside another if.
  • switch jumps to a matching case; remember break and default.
  • switch works with int/char, not float or string.
  • Ternary ?: is a one-line if-else.