StackTips

If-then and if-then-else Statements in Java

nilan avtar

Written by

Nilanchala,  5 min read,  3.18K views, updated on Sept. 17, 2023

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”

Beginner's Guide to Java Collections

This course covers the fundamentals of java collections framework covering different data structures in Java to store, group, and retrieve objects.

>> CHECK OUT THE COURSE
nilan avtar

Nilanchala

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!