If Else Condition in Java

  • Last updated Apr 25, 2024

In Java, the "if-else" statement is a conditional control structure that allows a way to make decisions in the code based on certain conditions. It allows you to specify different actions to take depending on whether a condition is true or false.

Here's the general syntax of if-else statement:

if(condition) {
   /* execute this if above condition is true. */
} else {
   /* execute this if above condition is false. */
}

In if-else statement the first block of code is executed if the condition evaluates to true else the second block of code is run if the condition evaluates to false. In other words, the else statement specifies a block of code to be executed when the if condition is false.

Example:

Here's an example program that demonstrates how the "if-else" statement works in Java:

import java.util.Scanner;

public class Example {

   public static void main(String[] args) {

      try (Scanner scanner = new Scanner(System.in)) {
         System.out.print("Enter your age: ");
	 int age = scanner.nextInt();

	 if (age >= 18) {
	    System.out.println("You are eligible to vote.");
	 } else {
	    System.out.println("You are not eligible to vote.");
	 }
       }

   }

}

This example program, takes input from the user using the Scanner class and store it in the variable age. The code then uses an "if-else" statement to check if the entered age is greater or equal to 18. If the condition age >= 18 results to true, the program will execute the code block inside the if statement, which prints "You are eligible to vote.". If the condition is false, the program will skip the if block and execute the code block inside the else statement, which prints "You are not eligible to vote."


If Statement

In Java, it is possible to use the "if" statement without an "else" clause.  This means the code block associated with the "if" statement will be executed, but if the condition is false, no alternative action will be taken.

Here's an example to demonstrate how if statement works in Java:
public class Example {

   public static void main(String[] args) {
		
      int number = 70;

      if (number > 0) {
         System.out.println("The number is positive.");
      }

      if (number % 2 == 0) {
         System.out.println("The number is even.");
      }
   }

}

In this program, the variable number is initialized with the value 70. We use the "if" statement to check various conditions. The first "if" statement checks if the number is greater than 0. If it is, the program will execute the code block inside the "if" statement and print "The number is positive." The second "if" statement checks if the number is divisible by 2. If it is, the program will execute the code block inside the "if" statement and print "The number is even."

The output of the above code is as follows:

The number is positive.
The number is even.


Else-If Statement

The "else if" statement is an extension of the "if" statement in Java. It allows to specify additional conditions to check when the initial "if" condition is false. If any of the "else if" conditions evaluate to true, the corresponding code block will be executed. If none of the conditions are true, an optional "else" block can be used to specify a default code block to execute.

Here's the syntax of the "else if" statement in Java:

if (condition1) {
    // Code to be executed if condition1 is true
} else if (condition2) {
    // Code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
    // Code to be executed if condition1 and condition2 are false and condition3 is true
} else {
    // Code to be executed if none of the conditions are true
}

Example:

Here's an example to demonstrate how else-if statement works in Java:

public class Example {

   public static void main(String[] args) {
      
      int number = 70;

      if (number > 0) {
         System.out.println("The number is positive.");
      } else if (number < 0) {
         System.out.println("The number is negative.");
      } else {
         System.out.println("The number is zero.");
      }

   }
}

In this example, the program checks the value of the number variable using the "else if" statement. If the number is greater than 0, it will execute the code block inside the first "if" statement and print "The number is positive." If the number is not greater than 0 but is less than 0, it will evaluate the second "else if" condition. If it is true, it will execute the code block inside the "else if" statement and print "The number is negative."