Java else-if Statements

java-Logo
Java Tutorials
Java Environment
Environment Setup
Declaration & Assignments
Flow Control
Miscellaneous
Java OOPs

else if statements in Java is like another if condition, it's used in the program when if statement having multiple decisions.

The basic format of else if statement is:

Syntax:
if(test_expression)
{
   //execute your code
}
else if(test_expression n)
{
   //execute your code
}
else
{
   //execute your code
}

Example of a Java Program to Demonstrate else If statements

Example:
public class Sample {

    public static void main(String args[]) {
        int a = 30, b = 30;

        if (b > a) {
            System.out.println("b is greater");
        }
        else if(a > b){
            System.out.println("a is greater");
        }       
        else {
            System.out.println("Both are equal");
        }
    }
}
Program Output:

java-else-if-statements


Scroll Back to Top