Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
5.3k views
in Computer by (49.6k points)

Explain the working of if-else if statement with a suitable example.

1 Answer

+1 vote
by (52.3k points)
selected by
 
Best answer

The ‘if-else if’ statement is an extension of the “if-else” conditional branching statement. When the expression in the “if’ condition is “false” another “if-else” construct is used to execute a set of statements based on the expression.

Syntax:

if(expression)

{ statements }

else if(expression)

{ statements }

else if(expression)

{ statements }\

Example: for ‘if else if’ statement

#include<iostream.h>

int main (void)

{

int per;

cout<<"Enter your Percentage::";

cin>>per;

If(per>=80)

{

cout<<"passed with Distinction"<<endI;

}

else if(per>=60)

{

cout <<"First Class"<<end1;

}

else if(per>=50)

}

cout<<"Second Class"<<end1;

}

else

{

cout<<"Third Class"<<end1;

}

return 0;

}

Result:

Enter your Percentage::60

First Class

In the above example, the ‘if else if’ statement is used to check multiple conditions based on the percentage of marks got, as input. The user entered percentage as 60, then first condition evaluated and returned with false. This resulted in checking for second if condition and resulted in true which give the print “first class”.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...