Java Journeys : Exploring Decision-Making Techniques.

Ritu Sitlani
Javarevisited
Published in
4 min readMar 13, 2024

Decision Making in Java

In Java, decision-making helps your program make choices and take different paths based on specific conditions.

Java Selection Statements :

In simpler terms, selection statements help your Java program choose what actions to take under different circumstances, making your code more flexible and responsive to different situations.

Types of Selection Statement :

if Statement :

The if statement in Java serves as the fundamental decision-making tool. It determines whether a specific statement or group of statements will execute based on a condition. In essence, if the condition evaluates to true, the associated block of statements executes; otherwise, it does not.

Let’s take a real life scenario where you decide to go for a run based on the weather :

package Java;
public class Statements {
public static void main ( String args[]) {
boolean isSunny = true;
if(isSunny){
System.out.println("Let's go for a run it's a sunny day");
}}}

Output

Let's go for a run it's a sunny day

Here, the condition after evaluation will be either true or false. if statement accepts boolean values — if the value is true then it will execute the block of statements under it.

if- else Statement :

As seen earlier, the if statement evaluates whether a condition is true. If it is indeed true, it proceeds to execute a designated block of code. The else statement triggers the execution of an alternative block of code when the condition in the if statement turns out to be false.

In extension of above example, where isSunny is false and else statement is used :

package Java;
public class Statements {
public static void main ( String args[]) {
boolean isSunny = false;
if(isSunny){
System.out.println("Let's go for a run it's a sunny day");

}
else {
System.out.println("it is not sunny outside, let's skip the run");
}
}}

Output :

it is not sunny outside, let's skip the run

nested- if Statement :

Nested if statements mean an if statement inside an if statement .

package Java;
public class Statements {
public static void main ( String args[]) {
boolean isSunny = true;
int temperature = 20;
if(isSunny){
if(temperature>15) {
System.out.println("Let's go for a run it's a sunny day");
}
else {
System.out.println("Let's skip the run today");
}
}
}}

Output :

Let's go for a run it's a sunny day

if- else- if Statements :

if else if in Java lets you check different conditions in order; each if statement is examined only if the one before it fails, allowing you to handle various possibilities in one code block.

package Java;
public class Statements {
public static void main ( String args[]) {
boolean isSunny = false;
boolean isRainy = false;
boolean isWarm = true;
if(isSunny){
System.out.println("Let's go for a run it's a sunny day");
}
else if(isRainy) {
System.out.println("Let's go for a walk, it's raining");
}
else if (isWarm) {
System.out.println("Let's go outside, it's warm today");

}
else {
System.out.println("let's stay at home!");
}}}

Output:

Let's go outside, it's warm today

Switch case :

In Java, a switch-case statement offers a method to choose from various options depending on the value of an expression.

package Java;
public class Statements {
public static void main ( String args[]) {
int temp = 30;
switch (temp){
case 10:
System.out.println("it's cool, let's go for a light walk");
break;
case 20:
System.out.println("it's warm, let's go for a light walk");
break;
case 30:
System.out.println("it's hot, let's go for a quick run");
break;
default:
System.out.println("let's stay at home!");

}
}}

Output:

it's hot, let's go for a quick run

Java supports three jump statements: break, continue and return :

Break: Stops the execution of a loop or switch statement.

Continue : Skips the current iteration of a loop and proceeds to the next iteration.

Return : Exits the current method and returns a value, if specified, to the caller.

int[] temperatures = {18, 22, 25, 28, 30, 32};

for (int temp : temperatures) {
if (temp < 20) {
System.out.println("Temperature " + temp + " is too low.");
continue; // Skip to the next temperature
}

if (temp > 30) {
System.out.println("Temperature " + temp + " is too high. Stopping processing.");
break; // Stop processing temperatures
}

System.out.println("Temperature " + temp + " is acceptable.");

if (temp == 25) {
System.out.println("Temperature " + temp + " is perfect. Let's go outside!");
return; // Exit the method
}
}

System.out.println("All temperatures processed.");
}
}

Output:

Temperature 18 is too low.
Temperature 22 is acceptable.
Temperature 25 is acceptable.
Temperature 25 is perfect. Let's go outside!

Conclusion :

Using these decision-making statements in Java, developers can make their programs flexible and responsive to different scenarios, which improves how well our Java programs work.

Explore More :

  1. java-basics-exploring-overflow-and-underflow-concepts
  2. java-basics-primitive-data-types-and-wrapper-classes-made-simple
  3. introduction-to-java-variables
  4. https://medium.com/javarevisited/java-operators-making-math-harder-

Thankyou!

linkedin , twitter

--

--

Ritu Sitlani
Javarevisited

Hi, I'm an Engineer sharing insights on software testing and Java - Learning along the way!