Java If Else Statement

The Java If Else Statement is an extension to the If statement. We already saw that the If clause only executes the code block when the test condition is true. And if the condition is false, it will terminate. In real-time, it is helpful instead required to perform something when the condition fails. For this, Java introduced If else statement.

In Java If else statement, the Else block executes the code when the condition fails. Let us see the syntax of the Java If Else statement:

Java If Else statement Syntax

The syntax behind this If Else Statement in Java Programming language is as follows:

if (Test_condition)  {
     //If the condition is TRUE, these lines will execute

else  {
     //If the condition is FALSE, these lines will execute

If the test_condition in the above syntax is true, True statements will run. Furthermore, if the test_condition result is false, the False statements will perform. Let us see the flow chart for a greater understanding.

Flow Chart of a Java If Else Statement

The following image displays you the Java If Else Statement flow chart

Flow Chart for Java If Else Statement

If the result of a test condition is true, STATEMENT1 will execute. Next,  STATEMENTN executed. If the result is False, STATEMENT2 will execute, followed by STATEMENTN. Here, irrespective of test results, STATEMENTN will execute. Because it is out of the if else code block, and it has no impact of the test condition result.

Java If Else Statement example

This Java program allows the user to enter his/her age. Next, Javac will verify whether he is qualified to vote or not using the If else statement. For the Java if else statement demo purpose, we are placing 4 different System.out.println functions. If the test results true, Java compiler will print two of them, and if it is false, print the other two.

package ConditionalStatements;

import java.util.Scanner;

public class IfElseStatement {
	private static Scanner sc;
	public static void main(String[] args) {
		int age;
		sc = new Scanner(;		
		System.out.println(" Please Enter you Age: ");
		age = sc.nextInt();
		if (age >= 18) {
			System.out.println("You are eligible to Vote."); //St1
			System.out.println("Please carry Your Voter ID to Polling booth");//St2
		else  {
			System.out.println("You are Not eligible to Vote.");//St3
			System.out.println("We are Sorry for that");//St4
		System.out.println("This Message is coming from Outside the IF ELSE STATEMENT");

ANALYSIS: Users will enter their age. If the age is greater than or equal to 18, then St1, St2 will print. If their age is less than 18, St3 and St4 will print as output. We have also placed one System.out.println function outside the If Else block. This line will execute irrespective of condition result. Let us given the age = 25. Condition (25 >= 18) is TRUE

Java If Else Statement 1

Let us enter age = 17 to fail the condition deliberately. Condition(17 >= 18) is FALSE.

Please Enter you Age: 
You are Not eligible to Vote.
We are Sorry for that

This Message is coming from Outside the IF ELSE STATEMENT