Pages

Wednesday, 9 May 2018

C else if Clause


C else if Clause

Till now we have learnt about the if statement, if-else statement, if-else nested statements and logical operators. Logical operators are good alternatives of nested if-else statements. But there are still some cases when we have to use nested if-else clause. So to make those situations a bit easy, Dennis Ritchie introduced else if clause.
What are else if clause?
Well they are no major difference between nested if-else and else if clause. As I told you earlier, the main problem with nesting is that it makes the program difficult to read. So to avoid that issue, else if clause is introduced.

General Syntax of else if clause is given below.

if (condition)
{
Statement 1
Statement 2 and so on
}

else if (condition)
{
Statement 1
Statement 2 and so on
}

else if (condition)
{
Statement 1
Statement 2 and so on
}

else
{
Statement 1
Statememt 2 and so on
}

Things to remember while using else if clause
You can use any number of else if clause in this ladder.
Usage of else in the last is completely optional. It means its up to you that you either want to include it or not in your program.
This does not destroy the readability of the program.
By using else-if clause, our program does not creep to the right due to indentation.
Remember by using else if clause, no change will occur in the execution of program. It is just the re-write of nested if-else clause. You can understand this point by watching below example.
C else if ClauseLets try to understand this clause with one program.

Question: Make one program to check the eligibility of student to take admission in a college. The student must fulfil at least one condition to take admission in the college.

Student should be male. His marks should be more than 80% in 12th and his age should be at least 18 years.
Student should be female. Her marks should be more than 75% in 12th and her age should be at least 17 years.
Student should be played at any national level game. Age and qualification doesn’t matter in this case.

#include <stdio.h>

void main ()
{
 int per, age;
 char gen, game;

 printf("Press M for male n F for female n Y for Yes in game n N for No in game n");
 printf("Enter gender, age and percentage in class 12th n");
 scanf("%c %d %d",&gen, &age, &per);
 printf("Did you play in any game at national level n");
 scanf("%c",&game);

 if ((age>=18 && per>=80 && gen=='M') || (age>=17 && per>=75 && gen=='F'))
  prinf("You can take admission in our college.");
 else if (game=='Y')
  printf("You can take admission based on sports Kota");
 else
  printf("You cannot take admission in our college.");
}
#include <stdio.h>

void main ()
{
 int per, age;
 char gen, game;

 printf("Press M for male n F for female n Y for Yes in game n N for No in game n");
 printf("Enter gender, age and percentage in class 12th n");
 scanf("%c %d %d",&gen, &age, &per);
 printf("Did you play in any game at national level n");
 scanf("%c",&game);

 if ((age>=18 && per>=80 && gen=='M') || (age>=17 && per>=75 && gen=='F'))
  prinf("You can take admission in our college.");
 else if (game=='Y')
  printf("You can take admission based on sports Kota");
 else
  printf("You cannot take admission in our college.");
}