Else If, Else-If Ladder and Nested If in C
In C programming, decision-making statements are used when we want the program to execute different blocks of code depending on a condition.
if else nested if else-if ladder1. Else If / Nested If
Sometimes, we need to check another condition only when the first condition is false. This can be achieved using an else block containing another if statement.
if statement placed inside another
if or else block is called a
nested if.
Basic Structure
if (condition1)
{
// statements
}
else
{
if (condition2)
{
// statements
}
}
Example: Check Whether a Number is a Natural Number and Even
#include <stdio.h>
void main()
{
int num = 13;
if (num <= 0)
{
printf("%d is not a natural number", num);
}
else
{
if (num % 2 == 0)
{
printf("%d is even natural number", num);
}
}
printf("\nRest of the program");
}
- The program first checks whether
num <= 0. - If true, it prints that the number is not a natural number.
- If false, the
elseblock is executed. - Inside the
elseblock, anotherifchecks whether the number is even.
For num = 13, the first condition is false and the
second condition 13 % 2 == 0 is also false.
Therefore, only:
Rest of the program
2. Else-If Ladder
When multiple conditions need to be checked one after another, we can use an else-if ladder.
If one condition is true, its corresponding block is executed and the remaining conditions are skipped.
General Structure
if (condition1)
{
// statement 1
}
else if (condition2)
{
// statement 2
}
else if (condition3)
{
// statement 3
}
else
{
// statement when all conditions are false
}
Example: Display Number in Words
#include <stdio.h>
void main()
{
int x;
printf("Enter any number 2, 4, 6 or 8 only: ");
scanf("%d", &x);
if (x == 2)
{
printf("TWO");
}
else
{
if (x == 4)
{
printf("FOUR");
}
else
{
if (x == 6)
{
printf("SIX");
}
else
{
if (x == 8)
{
printf("EIGHT");
}
else
{
printf("Invalid Input");
}
}
}
}
printf("\nRest of the program");
}
Same Logic Using an Else-If Ladder
#include <stdio.h>
void main()
{
int x;
printf("Enter any number 2, 4, 6 or 8 only: ");
scanf("%d", &x);
if (x == 2)
{
printf("TWO");
}
else if (x == 4)
{
printf("FOUR");
}
else if (x == 6)
{
printf("SIX");
}
else if (x == 8)
{
printf("EIGHT");
}
else
{
printf("Invalid Input");
}
printf("\nRest of the program");
}
3. Nested If vs Else-If Ladder
| Nested If | Else-If Ladder |
|---|---|
An if statement is placed inside another block. |
Multiple conditions are connected using else if. |
| Useful when the second condition depends on the first condition. | Useful when there are multiple alternative conditions. |
| Can result in multiple levels of indentation. | Usually easier to read. |
| Example: Check natural number first, then check whether it is even. | Example: Check whether a number is 2, 4, 6, 8, or invalid. |
4. Important Points Regarding Nested Blocks
-
Close the innermost block first.
For every opening curly brace{, there must be a corresponding closing curly brace}. -
Curly braces are optional for a single statement.
If anif,else, or loop contains only one statement, curly braces can be omitted. -
C is a free-form language.
Extra spaces, tabs, and indentation generally do not affect how C understands the program. However, proper indentation is strongly recommended because it makes the program easier to read and debug.
Example of Optional Curly Braces
if (x > 0)
printf("Positive");
The above is valid because the if contains only one
statement.
However, when multiple statements are required, curly braces should be used:
if (x > 0)
{
printf("Positive");
printf("\nNumber is greater than zero");
}
5. Practice Questions
Solve the following programs using only Nested If and Else-If Ladder.
Practice 1: Biggest of Three Numbers
Write a C program to determine the biggest out of three user-entered numbers.
Example:
Enter three numbers:
25
10
18
Biggest number = 25
Hint: Use comparisons between the three numbers. Try solving the problem first using nested if and then using an else-if ladder.
Practice 2: Vowel or Consonant
Write a C program to accept any English alphabet from the user and display whether it is a vowel or consonant.
The user can enter both uppercase and lowercase alphabets.
Vowels are:
A, E, I, O, U and
a, e, i, o, u.
Example 1:
Enter an alphabet: E
E is a vowel
Example 2:
Enter an alphabet: k
k is a consonant
Example 3:
Enter an alphabet: 5
Invalid input
if.
Then solve it again using an else-if ladder.
Additional Practice
- Write a C program to check whether a number is positive, negative, or zero using an else-if ladder.
- Write a C program to accept a number from 1 to 7 and display the corresponding day of the week.
- Write a C program to accept a student’s marks and display the grade using an else-if ladder.
- Write a C program to check whether a given year is a leap year using nested if.
- Write a C program to find the smallest among three numbers using nested if.
6. Quick Revision
- Nested if: An if statement inside another if/else block.
- else-if ladder: Used to check multiple conditions one after another.
- Conditions are generally checked from top to bottom.
- Once a condition in an else-if ladder becomes true, the remaining conditions are skipped.
-
Every opening
{must have a matching closing}. - Curly braces can be omitted for a single statement, but using them consistently is recommended.
- Proper indentation does not change the meaning of C code, but it greatly improves readability.
Nested
if is useful when one decision depends on
another decision, while an else-if ladder is useful
when choosing between several alternative conditions.