If-Else Statements in Java With Real Time Example

Java If-else statement is used to check if a Boolean condition is true or false. The types of Java If statements are:

1. If statement

In Java, if statement is used to test whether a Boolean expression is true or false. If the condition is true, if the command will be executed and if the condition is false, then the first set of code after the end of the statement will be executed.

syntax:

if (condition) {
// executable statements
}

A simple example of if statement:

public class Test {

	public static void main(String[] args) {

		int x = 100;

		if (x == 100) {
			System.out.print("Value of x is equal of 100");
		}
	}
}

2. If-else statement

It is the extension of if statement. In this statement, if the Boolean expression is true then if the command of the code will be executed, and else block will be executed if the expression is false.

syntax:

if (condition) {
// statements

} else {
//statements
}

A simple example of if-else statement:

public class Test {
	public static void main(String[] args) {
		int x = 100;
		if (x < 200) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}
}

3. If-else-if ladder

This statement is used in order to test multiple expressions. Multiple numbers of if-else order can be used in a single time so this statement is also called an if-else-if ladder. In this statement, the order gets executed immediately after the condition is met and the rest is ignored.

If the condition is not met, the then else block of the order gets executed. This way the statement helps the developer to check a wide range of mutually exclusive possibilities.

Syntax: 

if (condition) {
// executable statements

} else if (condition) {
// executable statements

} else if (condition) {
// executable statements

}

A simple example of if-else if statement: 

public class Result {
	public static void main(String[] args) {

		int number = 96;

		if (number< 40) {
			System.out.println("Fail");
		} else if (number <= 59) {
			System.out.println("C Grade");

		} else if (number<= 79) {
			System.out.println("B Grade");
		}

		else if (number<= 95) {
			System.out.println("A Grade");
		}
		else {
			System.out.println("A+ Grade");
		}
	}
}

 

4. Nested if statement

In Java, if or else statement can be used inside another if or else statement. If the order gets executed as long as the condition is met and if the condition is not met else order gets executed.

Syntax:

if(condition){
if(condition){
} else { // executable statements
}else{ // executable statements
}
A simple example of nested if statement:

public class AgeGroup {
	public static void main(String[] args) {
		int age = 45;

		if (age <= 25) {
			if (age <= 20) {
				System.out.println("Best time to finished Colllege education");
			} else
				System.out.println("Best time to start own business");
		} else {
			System.out.println("You can do work for others");
		}
	}
}

 

Real Time Example of If-Else Statements in Java

Q. Write a Java program that will display different messages depending on your age. Your program should ask the user for his/her name and their age in years and give one or more answers from the following ones below:

if the age of the user is less than 16, the program should print on the screen “You are not allowed to drive at the moment”.

if the age of the user is less than 18, the program should print on the screen “You are not allowed to vote at the moment”.

if the age of the user is less than 25, the program should print on the screen “You are not allowed to rent a car at the moment”.

if the age of the user is greater than or equal with 25, the program should print on the screen “You can do anything that is legal”.

import java.util.Scanner;

public class AgeGroup {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("What is your age? ");
		int age = sc.nextInt();

		if (age < 16) {
			System.out.println("You are not allowed to drive at the moment");
			int option1 = sc.nextInt();

		} if (age < 18) {
			System.out.println("You are not allowed to vote at the moment");
			int option2 = sc.nextInt();

		} if (age < 25) {
			System.out.println("You are not allowed to rent a car at the moment");
			int option3 = sc.nextInt();
		} else if (age >= 25) {
			System.out.println("You can do anything that is legal");
		}
	}
}

Check also: ArrayList in Java with Real Time Example

Leave a Reply

Your email address will not be published. Required fields are marked *