Decision Constructs in Java

Gaurav Shah
5 min readMay 18, 2024

--

Introduction

Decision constructs are fundamental building blocks in programming that allow developers to control the execution flow based on certain conditions. In Java, the decision constructs empower developers to write flexible and efficient code by incorporating conditional statements and control flow structures. In this blog, we’ll delve into the various decision constructs available in Java, their syntax, and examples to illustrate their usage.

Decision Constructs

The if statement

The if statement is the most basic decision construct in Java. It allows developers to execute a code block only if a specified condition evaluates to true. Let’s understand it with an example:

class IfStatement {
public static void main(String[] args) {
int i = 10;
if (i > 5) {
// code to be executed if the condition (i > 5) is true
System.out.println("i is greater than 5");
}
if (i > 15) {
// code to be executed if the condition (i > 15) is true
System.out.println("i is greater than 15");
}
}
}

Output

i is greater than 5

The if-else statement

The if-else statement extends the functionality of the if statement by providing an alternative block of code to execute if the condition evaluates to false. Let’s understand it with an example:

class IfElseStatement {
public static void main(String[] args) {
int i = 10;
if (i < 5) {
// code to be executed if the condition (i < 5) is true
System.out.println("i is less than 5");
} else {
// code to be executed if the condition (i < 5) is false
System.out.println("i is greater than 5");
}
}
}

Output

i is greater than 5

The if-else-if statement

When dealing with multiple conditions, the if-else-if statement allows developers to test various conditions sequentially until one evaluates to true, executing the corresponding code block. Let’s understand it with an example:

class IfElseIfStatement {
public static void main(String[] args) {
int i = 15;
if (i < 5) {
// code to be executed if the condition (i < 5) is true
System.out.println("i is less than 5");
} else if (i < 10) {
// code to be executed if the condition (i < 10) is true
System.out.println("i is less than 10");
} else {
// code to be executed if all conditions are false
System.out.println("i is greater than 10");
}
}
}

Output

i is greater than 10

Things to keep in mind while using the if statement

  1. One course of action for an if construct (using only if and omitting the else part) is acceptable but you can’t define the else part for an if construct, skipping the if code block. The code won’t compile.
  2. Using if(testValue == true) is the same as using if(testValue).
    Similarly, if(testValue == false) is the same as if(!testValue).
  3. An if block is marked by enclosing one or more statements within a pair of curly braces { }. An if block will execute a single line of code with no braces but will execute an unlimited number of lines if they’re contained within a block (defined using braces).
    The braces are optional if there’s only one line in the if statement.
if, if-else, and if-else-if statement

Ternary construct

The ternary operator is a compact decision-making construct in Java. It allows you to write concise conditional expressions with a compact syntax. Unlike if-else statements, which are statements, the ternary operator is an expression, meaning it returns a value.
Ternary operators are useful to assign values or make decisions based on simple conditions in a single line, making your code more concise and readable.
Syntax of the ternary operator:

result = (condition) ? expression1 : expression2

Here’s how it works:
The condition is evaluated first.
If it’s true, expression1 is executed. Otherwise, expression2 is executed.
The type of the result produced by the ternary operator is determined by the common type of the two expressions.
Let’s understand it with an example:

class TernaryConstruct {
public static void main(String[] args) {
int age = 25;
String message;
message = (age >= 18) ? "You are an adult" : "You are not an adult";
System.out.println(message);
}
}

Output

You are an adult

The switch statement

The switch statement is useful particularly when you have multiple possible execution paths based on the value of a variable. It evaluates the variable and executes the corresponding case block. Let’s understand it with an example:

class SwitchStatement {
public static void main(String[] args) {
int dayOfWeek = 3;
String dayName;
switch (dayOfWeek) {
case 1:
// code to be executed if dayOfWeek equals 1
dayName = "Monday";
break;
case 2:
// code to be executed if dayOfWeek equals 2
dayName = "Tuesday";
break;
case 3:
// code to be executed if dayOfWeek equals 3
dayName = "Wednesday";
break;
case 4:
// code to be executed if dayOfWeek equals 4
dayName = "Thursday";
break;
case 5:
// code to be executed if dayOfWeek equals 5
dayName = "Friday";
break;
case 6:
// code to be executed if dayOfWeek equals 6
dayName = "Saturday";
break;
case 7:
// code to be executed if dayOfWeek equals 7
dayName = "Sunday";
break;
default:
// code to be executed if none of the above cases are true
dayName = "Invalid day";
}
System.out.println("Today is " + dayName);
}
}

Output

Today is Wednesday

Things to keep in mind while using the switch statement

  1. A switch statement accepts arguments of types char, byte, short, int, and String. It also accepts arguments and expressions of enum, Character, Byte, Integer, and Short type.
  2. The switch statement doesn’t accept arguments of type long, float, double, or any object besides String.
  3. Apart from passing a variable to a switch statement, you can also pass an expression to a switch statement as long as it returns one of the allowed types.
  4. Values passed to the label case of a switch statement should be compile-time constants. That is the value should be known at the time of code compilation. If using an expression in the switch case, the variables must be declared as final so that it’s constant and can’t be manipulated.
  5. null is not allowed as a case label. Code that compares the variable passed to the switch statement with null won’t compile.
  6. When a break statement is reached, the switch terminates, and the flow control jumps to the next line following the switch statement.
  7. Not every case needs to contain a break. If no break appears, the flow control will fall through to subsequent cases until a break is reached.
  8. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Conclusion

Decision constructs are essential tools in Java for controlling the flow of your programs. Understanding and effectively utilizing if, if-else, if-else-if, switch, and the ternary operator allows you to write more flexible and efficient code. You can handle complex decision-making processes and build robust Java applications by mastering these constructs.

Oracle Certified Associate (OCA) certification overview:
Oracle Certified Associate: Java SE 8 Programmer

For more Java and Spring-Boot-related blogs, check out my profile:
https://medium.com/@gauravshah97

Reach me out at:
https://www.linkedin.com/in/gauravshah97/

--

--