Java

Examples of Decision Making in Java

This article explains the concept of Examples of Decision Making in Java. Basically, decision-making in a code is required whenever there is two or more course of action depending on a condition. So, in Java, we can perform decision-making using the if…else statement. Also, we can also use the ternary operator in Java if we need to choose one of the two values. The following section demonstrates several examples of decision-making in Java.

Examples of Decision Making in Java

The following code shows an example of using ternary operator. In fact, it performs decision making in a single statement. For example, in the code given below, first, the condition is checked. So, in case, the condition is true, the value on left hand side of : is assigned to the variable b. Otherwise, the value at right hand side is assigned.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		int age=24;
		String b=(age<18?"Minor":"Adult");
		System.out.println("Age = "+age+", Person: "+b);
		age=7;
		b=(age<18?"Minor":"Adult");
		System.out.println("Age = "+age+", Person: "+b);
	}
}

Output

Age = 24, Person: Adult
Age = 7, Person: Minor

Another example of conditional statement for decision making is given below. While, the code finds the smallest of three numbers. As can be seen, nested if…else statements are used here.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		int p, q, r;
		p=78; q=45; r=19;
		System.out.println("p = " +p+", q = "+q+", r = "+r);
		if(p<q)
		{
			if(p<r)
			{
				System.out.println(p+ " is smallest!");

			}
			else
			{
				System.out.println(r+ " is smallest!");
			}
		}
		else
		{
			if(q<r)
			{
				System.out.println(q+ " is smallest!");

			}
			else
			{
				System.out.println(r+ " is smallest!");
			}
		}
	}
}

Output

p = 78, q = 45, r = 19
19 is smallest!

Besides using the nested if statement, we can also use the compund conditions. Also, it simplifies the code and imroves the readability. In order to formulate the compund condition, the logical AND (&&) operator is used here.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		int p, q, r;
		p=8; q=5; r=19;
		System.out.println("p = " +p+", q = "+q+", r = "+r);
		if((p<q) && (p<r))
		{
			System.out.println(p+ " is smallest!");
		}
		else
		{
			if((q<p) && (q<r))
			{
				System.out.println(q+ " is smallest!");

			}
			else
			{
				System.out.println(r+ " is smallest!");
			}
		}
	}
}

Output

p = 8, q = 5, r = 19
5 is smallest!

package welcome;
public class Welcome {
	public static void main(String[] args) {
		int p=59;
		if(p%2==0)
		{
			System.out.println(p+ " is Even Number!");
		}
		else
		{
			
			System.out.println(p+ " is Odd Number!");
		}
	}
}

Output

59 is Odd Number!

Using Switch Statement for Decision Making in Java

Specifically, we use the switch statement when there are many conditions to check in a single if statement. In this case, we can replace the if statement by a switch statement. Basically, switch statement involves evaluating an expression. Further, the value of expression is compared with each case label. As soon as, any case label matches with value of expression, the statements following that case label will execute.

However, if none of the case label matches with r=the expression value, the switch statement look for the default label. In this case, all the statements followed by default execute. However, if default statement is not present, the switch statement exits. The following code shows an example of using switch statement. Although, it is also possible to use an if…else ladder for the example, the use of switch statement simplifies the code.

package welcome;
public class Welcome {
	public static void main(String[] args) {
		int error_code=403;
		String message="";
		switch(error_code)
		{
		case 400:
			message="bad request!";
			break;
		case 401:
			message="unauthorized!";
			break;
		case 402:
			message="payment required!";
			break;
		case 403:
			message="Forbidden!";
			break;
		case 404:
			message="Not Found!";
			break;
		default:
			message="Wrong Value!";
		}
		System.out.println("Error: "+message);
	}
}

Also, we prefer the switch statement when the conditions involve checking the equality. As can be seen in the code, at each case label, the value of variable error_code is compared with the corresponding case label. Accordingly, the variable message is updated.

Output

Error: unauthorized!


Further Reading

Evolution of JavaScript from ES1 to ES2020

Understanding Document Object Model (DOM) in JavaScript

Introduction to Programming in TypeScript

Creating Classes in TypeScript

Working with Arrays in TypeScript

Significance of Tuples in TypeScript

Explaining Interfaces in TypeScript with Examples

How to Create and Use Arrow Functions in TypeScript

Understanding JSON Web Tokens

Java Practice Exercise

Introduction to HTML DOM Methods in JavaScript

Changing Style of HTML Elements using getElementsByClassName() Method

Show or Hide TextBox when Selection Changed in JavaScript

JavaScript Code Examples

Callback Functions in JavaScript

JavaScript Code for Event Handling

What is Asynchronous JavaScript?

Understanding HTTP Requests and Responses

Basics of Java Programming

Leave a Reply

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