The if-else ladder is used when there are multiple conditional statements that may all evaluate to true, yet you may want only one if statement’s body to execute. You can use an “else-if” statement following an if statement and its body.
That way, if the first statement is true, the “else if” will be ignored, but if the if statement is false, it will then check the condition for the else if statement. if the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed.
GET INSTANT HELP FROM EXPERTS!
- Looking for any kind of help on your academic work (essay, assignment, project)?
- Want us to review, proofread or tidy up your work?
- Want a helping hand so that you can focus on the more important tasks?
Hire us as project guide/assistant. Contact us for more information
Syntax for If else ladder:
If (condition)
{
statements;
}
else if (condition)
{
statements;
}
else if (condition)
{
statements;
}
.
.
.
.
else
{
statements;
}
For example, you can take the grades of various students and assign them various grades using the If else ladder.
Let’s say:
Grade A+ means percentage over 75
Grade A means 75 > percentage >= 60
Grade B means 60 > percentage >= 50
Grade C means 50 > percentage >= 40
Fail means 40 > percentage
Percentage of each student can be calculated using the formula (total obtained marks/total maximum marks) * 100
Once the percentage is obtained, the grade of the student can be determined (by referring to the table above) using the If else ladder.
If percentage is greater than 75 display grade as A+
else if percentage is less than 75 and greater than or equal to 60 then display grade as A
else if percentage is less than 60 and greater than or equal to 50 then display grade as B
else if percentage is less than 50 and greater than or equal to 40 then display grade as C
else display result as Fail.
StudyMumbai.com is an educational resource for students, parents, and teachers, with special focus on Mumbai. Our staff includes educators with several years of experience. Our mission is to simplify learning and to provide free education. Read more about us.
Leave a Reply
You must be logged in to post a comment.