If-else statement in java is used for conditional checks for decision making. You can have multiple hierarchies of if-else statements. Once any of the if or else-if condition satisfies it executes the block of statements corresponding to it. An else statement is optional. If none of if or else-if statements satisfies then else statement gets executed. Once the statements inside if-else block is executed it continue program normally with rest of the statement/block.

if-then and if-then-else Statements

For example, if you want check a number N1 is greater than or less to another number N2, then you will have to use if-else block. Below example will demonstrates the usages of if else statements.

import java.util.Scanner;

public class JavaTest {

	public static void main(String[] args) {

		int N1 = 10;
		System.out.println("Enter Value for N2:");
		Scanner scanner = new Scanner(System.in);
		int N2 = scanner.nextInt();

		if (N1 > N2) {
			System.out.println("N1 is greater");
		} else if (N1 == N2) {
			System.out.println("N1 and N2 are equal");
		} else {
			System.out.println("N2 is greater");

Well, let’s give a closure look at the above code. Here In this example we are defining two integer variables N1 and N2. N1 is predefined with value 10 and for N2 we are asking users input. Scanner is used to accept input from user. More about scanner we’ll cover in later sections of tutorials.

Test Case1- For user entered value for N2 = 8

The fist if condition is checking whether, the value of N1 is greater than value of N2. As 10 is greater than 8, it satisfies the condition and executes the associated block of code. Here in this case it prints “N1 is greater”

Test Case2- For user entered value for N2 = 10

It will fail the first if condition check and it will check for the else if block. The else-if block condition satisfies and it executes the associated block of code. Here in this case it prints “N1 is equals to N2”

Test Case2- For user entered value for N2 = 20

Both if and else-if will fail for the value of N2 as 20. Then it executes the else statement and it will print “N2 is greater”

This article is about: Core Java, Java Basics,


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.