Else If
This is used when we want to check further condition when the given condition is returned false.
//Example of else if
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”);
}
else if ladder
When the checking of another condition when a given condition is returned false and this process is propagating, then it makes a ladder like pattern. It is known as else…if ladder
//Example of else if
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”);
}
Practice) (Solve only using Nested if and else…if ladder)
1) Write C program to determine biggest out of three user entered numbers
2)Write C Program to accept any English alphabet from the user and display whether it is vowel or consonant. User can enter both upper and lower case alphabets.
Important points regarding nested blocks:
1. when we are creating nested blocks, the inner most block to be closed first. For each opening brace there must be ending brace.
2. putting single statement under Curly brace is optional when we are defining block of control statements.
3. C Lang is free-form language. extra spaces, tabs, indents are ignored.