As we saw earlier, the if statement can have an else part attached to it. This else part can in turn contain another if statement. The if statement, coded in this manner sets up a sequence of condition’s. Each of these conditions is tested one after the other starting from top- to-bottom. The body part of the if statement is executed only if the condition executes to True; after which the statements continue to be executed in that order.
A code-snippet displaying the use of if-else-if statement is displayed below:
//Example 1
if(color_paper == YELLOW)) {
System.out.println(“The color of the paper is yellow.”);
}
else if(color_paper == RED) {
System.out.println(“The color of the paper is red.”);
}
//Example 2
if(employee.is.Engineer()) {
System.out.println(“The employee Is an Engineer”);
}
else if(employee.is.Manager()) {
System.out.println(“The employee is a Manager”);
}
else {
System.out.println(“the employee is Chairman of company”);
}
Continue reading Java Control Statement – if-else-if Statement