How to use Java conditional statements effectively
Our first tutorial on learning Java left room for improvement. Here are ways to use Java's if, else and switch statements more effectively.
The first iteration of our rock-paper-scissors application worked well, but the code certainly left some opportunities for refactoring, especially in terms of using Java conditional statements effectively.
It's often asserted that Java has an additional else...if statement. That isn't technically true.
Working with if, else and switch statements
The Java language provides three constructs for performing conditional logic:
- The if statement
- The else statement
- The switch statement
It's often asserted that Java has an additional else...if statement, which isn't technically true. You can nest if statements inside of an else block, and the code to do so puts the else and if statements right next to each other, but that's just happenstance. The else...if statement isn't a special Java construct in itself.
The syntax for an if…else statement looks like this:
if (boolean condition) {
} else {
}
The first code block executes if the boolean condition is true, and the second code block executes if the boolean condition is false. Here's how the rock-paper-scissors (or roshambo) application would look if we introduced the else keyword into the mix.
import javax.swing.*;
public class Roshambo {
public static void main(String args[]) {
String prompt = "Will it be rock, paper or scissors?";
String clientGesture = null;
String serverGesture = "rock";
String outcome = "error";
clientGesture = JOptionPane.showInputDialog(prompt);
if (clientGesture.equalsIgnoreCase("rock")) {
outcome = "tie";
} else if (clientGesture.equalsIgnoreCase("scissors")) {
outcome = "loss";
} else if (clientGesture.equalsIgnoreCase("paper")) {
outcome = "win";
}
JOptionPane.showMessageDialog(null, outcome);
}
}
When the code is run, the UI looks exactly the same as before, although the implementation is now much cleaner and more efficient (Figure 1).
Figure 1. Swing-based components providing a UI for the roshambo application.
Working with Java's switch statement
Another of the Java conditional statements, the switch statement, can be useful when a variety of different conditions might be true based on the value of a single char, integer or a String. With the roshambo application, the clientGesture String can take on the values of rock, paper or scissors, and depending upon which is selected, the variable named outcome is assigned a different value. This is a perfect scenario for using a switch statement. Replacing the if-based conditional logic in the current application with a switch would look like this:
import javax.swing.*;
public class Roshambo {
public static void main(String args[]) {
String prompt = "Will it be rock, paper or scissors?";
String clientGesture = null;
String serverGesture = "rock";
String outcome = "error";
clientGesture = JOptionPane.showInputDialog(prompt);
switch (clientGesture) {
case "rock":
outcome = "tie";
break;
case "paper":
outcome= "win";
break;
case "scissors":
outcome = "loss";
break;
default:
outcome="error";
}
JOptionPane.showMessageDialog(null, outcome);
}
}
A few points about the switch
A few things to know about the switch statement:
- Only since Java 7 has it been possible to switch on String values
- Once a condition is met, all subsequent conditions are executed unless a break statement is used to exit the switch block
- An optional default block can be used as a catch-all to handle the situation where none of the predicted conditions are met
In the grand scheme of things, all computer programs come down to three key processes:
- Handling data through variables
- Conditional logic
- Iterative looping
As mentioned earlier, only three constructs in Java pertain to performing conditional logic: the if, else and switch statements. Once you have mastered them, you have mastered the art of using Java conditional statements.
Which software development language do you think compliments Java the most? Let us know.
Modern programming languages made easy for Java developers
For those interested in learning Java, or for those who know Java and are interested in leveraging those skills in order to learn a complimentary language like JavaScript or Ruby, TheServerSide is providing a number of problem-driven tutorials that will help you master the fundamentals of these languages.
The catalog of tutorials currently includes the following:
- Roshambo: The programming problem to be solved
- An introduction to Java programming
- Working with advanced conditional statements in Java
- An introduction to Ruby programming
- Working with advanced conditional statements in Ruby
- An introduction to JavaScript programming
- Working with advanced conditional statements in JavaScript