C else if Clause

Till currently, we’ve learnt concerning the if statement, if-else statement, if-else nested statements and logical operators.

Logical operators are sensible alternatives to nested if-else statements. however there are still some cases once we need to use nested if-else clause.

therefore to create those things a touch straightforward, Dennis Ritchie introduced else if clause.

What is else if clause?

Well, they’re no major distinction between nested if-else and else if clause. As I told you earlier, the most drawback with nesting is that it makes the program troublesome to browse.

therefore to avoid that issue, else if the clause is introduced.

General Syntax of else if the clause is given below.

if (condition)
one
Statement a pair of and then on
}

else if (condition)
one
Statement a pair of and then on
}

else if (condition)
one
Statement a pair of and then on
}

else
one
Statement a pair of and then on
}

Things to recollect whereas victimization else if clause

You can use any range of else if clause during this ladder.

Usage of else within the last is totally nonobligatory. It means that it’s up to you that you just either need to incorporate it or not in your program.

This doesn’t destroy the readability of the program.

By victimization the else-if clause, our program doesn’t creep to the proper thanks to indentation.

Remember by victimization else if clause, no amendment can occur within the execution of the program.

it’s simply the re-write of the nested if-else clause.

you’ll be able to perceive now by look below the example.

Let’s try and perceive this clause with one program.

Question: build one program to examine the eligibility of the student to require admission in a very faculty.

the coed should fulfil a minimum of one condition to require admission within the faculty.

A student ought to be male. His marks ought to be over eightieth in twelfth and his age should be a minimum of eighteen years.

A student ought to be feminine. Her marks ought to be over seventy-fifth in twelfth and her age should be a minimum of seventeen years.

A student ought to vie at any national-level game. Age and qualification don’t matter during 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.");
}

Output

Press M for male
F for female
Y for Yes in game
N for No in game 
Enter gender, age and percentage in class 12
M
18
68 
Did you play in any game at national level
Y 
You can take admission based on sports Kota 

Explanation

In the starting we tend to gave some directions to user that however he/she ought to enter the information in our program.

After that we tend to written the message and take some details from the coed.

Now primarily we’ve a pair of conditions. initially we’ve to examine the coed if he/she eligible for general kota and within the last we have to check if he/she is eligible for sports kota.

To make things a touch clear I checked the results for general kota by victimization if keyword.

I combined the conditions by victimization logical operators to create the program a touch compact.

After that by victimization else if clause I even have checked if the coed is eligible for sports kota.

If nothing works then I even have conjointly given the default else block to print the message “You cannot take admission in this college”.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

ten + 13 =

Recent Content