Java

if-else and else-if Statements in Java

Java provides some decision-driven statements that are used to control the flow of the program based on some conditions. Using the if-else statements a program decides which part of the program should execute. Within any Java program, if and else statements can be used combinedly so that a program must execute in either case. This write-up will provide a step-by-step guide for the if-else and else-if statements.

This article presents a comprehensive overview of the following concepts:

  1. What is If, if-else, and else if statement
  2. Basic syntax of If, if-else, and else if statements in Java
  3. How to Use If, if-else, and else if statements in Java

Let’s start!

What is if Statement in Java

It is used to test whether the condition is true or not and as a result it returns a Boolean value. The code within the body of “if-statement” executes only when the Boolean expression returns a true value.

Syntax
The basic syntax of the “if statement” will look something like this:

 if (condition)
 {
  //statement(s);
 }

The code written inside the body of the if-statement will execute only if the condition is true.

How if statement works
The below snippet shows how if-statement works in Java.

Example
This example takes a value from the user and prints “You Entered an Even Number” if the entered number is divisible by 2.

public static void main(String[] args) {
 float number;
 System.out.println("Enter a Number");
 Scanner  scan = new Scanner(System.in);
 number = scan.nextInt();
 if(number % 2 == 0)
  {
   System.out.println("You Entered an Even Number");
  }
}

The above code snippet provides the following output:

Now, what if someone enters an odd number, how if statement deals with a false condition? Let’s try it!

The output shows that the if-statement doesn’t deal with the false statements.

What is if-else Statement in Java

To address the false statements Java provides else statement. So the combination of if and else statements can tackle both true and false statements.

Syntax
The basic syntax of the if-else statement will be:

if(condition)
{
statement or block of statements;      //condition = true
}
else
{
statement or block of statements;      //condition = false
}

How if-else works in Java
Let’s extend the above example a little bit more and to test how else statement works:

Example
The below snippet will execute the body of if-statement when the number is divisible by 2 and an else statement will execute if the number is not divisible by 2:

public static void main(String[] args) {
 float number;
 System.out.println("Enter a Number");
 Scanner  scan = new Scanner(System.in);
 number = scan.nextInt();
 if(number%2 ==0)
  {
   System.out.println("You Entered an Even Number");
  }
 else
  {
   System.out.println("You Entered an Odd Number");
  }
}

Now if a user will enter an odd number then it will be addressed in the else part:

Let’s move one step further to understand what is else-if and how it works in Java.

What is else-if in Java

Now, what if we want to perform multiple tasks based on the different conditions? Well! In java, we can use the else-if statement in such situations. The below snippet shows the basic syntax of  else-if statement in java:

if (condition)
{
  statement(s);
}
else if(condition)
{
  statement(s)
}
else
{
  statement(s)
}

Example
Let’s consider an example that takes an input from the user:

When the user entered number is divisible by 3 then it prints “the entered number is divisible by 3”

When the number is divisible by 5 then it prints “the entered number is divisible by 5”

else it should print “you entered a number neither divisible by 3 nor by 5”:

public static void main(String[] args) {
 double number;
 Scanner  scan = new Scanner(System.in);
 System.out.println("Enter a Number");
 number = scan.nextInt();
 if(number%3 ==0)
 {
  System.out.println("The number is divisible by 3");
 }
 else if(number%5 ==0)
 {
 System.out.println("The number is divisible by 5");
 }
 else
 {
 System.out.println("You entered a number that is neither divisible by 3 nor divisible by 5");
 }
}

The above code snippet provides the following output:

The output authenticates that the above given code is working properly.

Conclusion

The Java if statement gets executed only if the specified condition is true, on the other hand, the else condition gets executed if the condition is false. Moreover, to tackle more than two conditions “else if” can be used. This article presents a thorough overview of what is if-else, and else-if statements and how to use these statements in Java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.