Java else-if Statements

thumbnail

else if statements in java is similar to if condition, it’s used in the program when if statement have more than one decisions.

The simple format of else-if statement is:

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

Example of a Java program to show else-if statements

public class SampleElseIf {

    public static void main(String args[]) {
        int valueA = 25, valueB = 25;

        if (valueB  > valueA) {
            System.out.println("valueB  is greater");
        }
        else if(valueA > valueB ){
            System.out.println("valueA is greater");
        }       
        else {
            System.out.println("valueA and valueB are equal");
        }
    }
}
Back To Top