The if and if-else constructs we have seen so far allow us to test a single condition and execute one set of steps if it is true and another set of steps if it is false. But often there are situations where we need to test multiple conditions.
Take the example of your school day, if it is maths class you will study maths, if it is physics class you will study physics, if it is recess time you will take a break, talk to your friends, have your tiffin, if it is games time then you will go out and play games and so on. In such scenarios, you must test multiple conditions and then take a decision.
The programming construct in Java which helps us with such decision making is the if-else-if ladder.
Its syntax is this:
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
..
..
else
statement;
The if statements are executed from top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. The final else acts as a default condition as in if all other conditional tests fail, then the last else statement is performed. If you don’t write a final else and all other conditions are false, then none of the statements from the if-else-if ladder will get executed.
Let’s try writing a program using if-else-if ladder. Our program will ask the user to enter her marks. It will then output the grade obtained by the user based on her marks as per this table:
Marks | Grade |
---|---|
0-34 | Fail |
35-59 | C grade |
60-79 | B grade |
80-94 | A grade |
95+ | A+ grade |
Here is our Java program demonstrating if-else-if ladder:
/**
* This program computes grade of student based on marks obtained.
*
* @author KnowledgeBoat
* @version 1.0
*/
import java.util.Scanner;
public class KboatGradeObtained
{
public void computeGrade() {
Scanner in = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = in.nextInt();
if (marks < 35)
System.out.println("Fail");
else if (marks < 60)
System.out.println("C grade");
else if (marks < 80)
System.out.println("B grade");
else if (marks < 95)
System.out.println("A grade");
else
System.out.println("A+ grade");
System.out.println("if-else-if demo completed.");
}
}
In the if-else-if ladder, our program will first check if the marks are less than 35. If this condition is false, the program control will move to the next condition and check if marks are less than 60. If this condition is also false, the program control will move to the next condition and check if marks are less than 80. If this condition is also false, the program control will move to the next condition and check if marks are less than 95. If this condition is also false, the program control will enter the else part and execute this println statement System.out.println("A+ grade");
to print A+ grade on the screen. At any point in stepping through the if-else-if ladder, if any of the condition tests out to be true, the corresponding println statement will get executed and the rest of the ladder will be skipped. Program control will move to this statement System.out.println("if-else-if demo completed.");
after the if-else-if ladder.
Below is the output of the program. As you can see, our program correctly tells the grade for each input.
This completes our discussion of if statement, next we will look at the switch statement.