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 the If else statement. In the If else statement, the Else block executes the code when the condition fails and lets us see its syntax.
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 True_statements; } else { //If the condition is FALSE, these lines will execute False_statements; }
True statements will run if the test_condition in the above syntax is true. Furthermore, if the test_condition result is false, the False statements will perform. Let us see the flow chart for a greater understanding.
Java If Else Statement Flow Chart
The following image displays the Java If Else Statement flow chart.

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 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 are true, the Javac 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.in); 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 and 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 the condition result. Let us give the age = 25. Condition (25 >= 18) is TRUE

Let us enter age = 17 to fail the condition deliberately. Condition(17 >= 18) is FALSE.
Please Enter you Age:
17
You are Not eligible to Vote.
We are Sorry for that
This Message is coming from Outside the IF ELSE STATEMENT