In this tutorial you will learn about the Java If Else If Ladder Statement and its application with practical example.
Java if else if ladder statement
In Java, if..else.if statement allows us to add an alternative set of test conditions in if else if ladder statement using else-if and single else statements for if condition. In such way if..else.if statement is used to select one among several blocks of code to be executed.
Java if..else…if Statement Flow Diagram
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition1) { // statement(s) } else if(condition2){ // statement(s) } . . else if(conditionN){ // statement(s) } else { // statement(s) } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Main { public static void main(String[] args) { int a = 10; int b = 10; System.out.println("W3Adda - Java If else if Statement"); if (a > b) { System.out.println("a is greater than b"); } else if(a == b){ System.out.println("a and b are equal"); } else{ System.out.println("b is greater than a"); } } } |
Output:-