5.10 – else if selection statement ( else if ladder )

by subbu on September 22, 2013

else-if selection statement in C language (else-if ladder):

  • Simple if selection statement is used to check multiple conditions, out of which any number of conditions may be true.
  • else-if ladder is a selection statement used to check multiple conditions, out of which any single or no condition must be true.
  • In else-if ladder, condition are checked one by one, if any condition is true then statement(s) under the conditional statement is executed and left all the structure is skipped from the execution.
  • If no condition is true then the else part is executed.
  • Defining else part in else-if ladder is optional.

else if ladder in C language

Note: There should be a space among else and if in the structure

#include<stdio.h>
int main()
{
if(10<20)
  printf("One");/* selected */
if(40>80)
  printf("\nTwo");
if(20!=40)
  printf("\nFour");/*selected */
return 0;
}

Output:
One
Four

#include<stdio.h>
int main()
{
if(10<20)
  printf("One"); /*selected */
else if(40>80)
  printf("\nTwo");
else if(20!=40)
  printf("\nFour");
return 0;
}

Output:
One

Here the same program is written using both if and else-if selection statements but could find difference in execution.
While using simple if selection statement every condition is checked without considering the result of previous conditions.
While using else-if selection statement control flow came out of total structure on satisfying the first condition.

Specification:
Accept any integer and print whether the number is a single digit, two digit, three digit, biggest or smallest number.

Logic:

finding single digit, two digit , three digit number

Using simple if selection statement:

#include<stdio.h>
int main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n>=-9 && n<=9)
  printf("Sigle digit number");
if((n>=-99&&n<=10) || (n>=10&&n<=99))
  printf("Two digit number");
if((n>=-999&&n<=-100) || (n>=100&&n<=999))
  printf("Three digit number");
if(n>999)
  printf("Biggest number");
if(n<-999)
  printf("Smallest number");
return 0;
}

Here we can’t simply check the ranges because single digit numbers are existed within the range of two digit numbers and two digit numbers are existed within the range of three digit numbers. Hence we need to check both positive and negative ranges to identify two digit and three digit numbers.
Simple if selection statement is the reason to this difficulty because next conditions would be checked though the current condition is satisfied.

/*using else-if ladder */
#include<stdio.h>
int main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n>=-9 && n<=9)
  printf("Single digit number");
else if(n>=-99 && n<=99)
  printf("Two digit number");
else if(n>=-999 && n<=999)
  printf("Three digit number");
else if(n>999)
  printf("Biggest number");
else
  printf("Smallest number");
return 0;
}

Here the logic is so simplified because of else-if ladder. If we accept a single digit number then the first condition is satisfied and left all the structure is skipped. If we accept a two digit number then the control flow reaches to the second condition because the first condition is false and skips left all the structure. If we accept a three digit number then the control flow reaches to the third condition because the first and second conditions are false and skips left all the structure.

Specification:
Accept any character from the keyboard and print whether the given character is a vowel, consonant, digit or special symbol.

#include<stdio.h>
int main()
{
char ch;
printf("Enter any character:");
scanf("%c",&ch);
if(ch=='a' || ch== 'e' || ch=='i' || ch=='o' || ch=='u')
 printf("Vowel");
else if(ch>='b' && ch<='z') /*checked only when character is not vowel*/
 printf("Consonant");
else if(ch>='0' && ch<='9') /*checked only when character is not an alphabet*/
 printf("Digit");
else
 printf("Special symbol");
return 0;
}

Execution 1:
Enter any character: e
Vowel

Execution2:
Enter any character: p
Consonant

Execution3:
Enter any character: 8
Digit

Execution4:
Enter any character: $
Special symbol

Specification:
Accept the type of customer, principle amount, period of time and print the simple interest and the total amount must be paid to the financer. Here the interest rates per year are 5% for employees, 7% for farmers, 9% for business people and 12% for others

Logic: S.I= P*T*R/100

#include<stdio.h>
int main()
{
long int amount;
int time,ch;
float si,total;
printf("1. Employee\n2. Farmer\n3. Business Person\n4. Other\nEnter your choice 1..4:");
scanf("%d",&ch);
printf("Principle Amount:");
scanf("%ld",&amount);
printf("Period of time in years:");
scanf("%d",&time);
if(ch==1)
  si=(float)amount*time*5/100;
else if(ch==2)
  si=(float)amount*time*7/100;
else if(ch==3)
  si=(float)amount*time*9/100;
else
  si=(float)amount*time*12/100;
total=amount+si;
printf("Amount %ld",amount);
printf("\nSimple Interest %f",si);
printf("\nTotal amount %f",total);
return 0;
}

Execution:
1. Employee
2. Farmer
3. Business Person
4. Other
Enter your choice 1..4:3
Principle Amount:10000
Period of time in years:2
Amount 10000
Simple Interest 1800.000000
Total amount 11800.000000

Specification:
Accept the marks of a student in 6 subjects and print the total, average and result. Make sure that marks in any subject must not exceed to 100.

#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5,m6,tot;
float avg;
printf("Accept the marks of a student in 6 subjects:\n");
scanf("%d%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5,&m6);
tot=m1+m2+m3+m4+m5+m6;
avg=(float)tot/6;
printf("Total marks %d",tot);
printf("\nAverage marks %f",avg);
printf("\nResult:");
if(m1<50 || m2<50 || m3<50 || m4<50 || m5<50 || m6<50)
  printf("Fail");
else if(avg>=75)
  printf("Grade A+");
else if(avg>=60)
  printf("Grade A");
else if(avg>=50)
  printf("Grade B");
else
  printf("Grade C");
return 0;
}

Execution 1:
Accept the marks of a student in 6 subjects:
89 56 99 87 90 93
Total marks 514
Average marks 85.666664
Result:Grade A+

Execution 2:
Accept the marks of a student in 6 subjects:
90      55      78      90      12      88
Total marks 413
Average marks 68.833336
Result:Fail

Specification:
Accept any 3 digit number and print the biggest digit.

#include<stdio.h>
int main()
{
int n,first,second,third;
printf("Enter any three digit number:");
scanf("%d",&n);
third=n%10;
n=n/10;
second=n%10;
n=n/10;
first=n%10;
if(first==second && second==third)
  printf("All the digits are equal");
else if(first>second && first>third)
  printf("Biggest digit %d",first);
else if(second>third)
  printf("Biggest digit %d",second);
else
  printf("Biggest digit %d",third);
return 0;
}

Execution 1:
Enter any three digit number: 333
All the digits are equal

Execution 2:
Enter any three digit number: 452
Biggest digit 5

Previous post:

Next post: