First Steps in Java — Part 6

mike dietz
3 min readMay 6, 2020

--

Conditionals

1: Setup | 2: Variables, Data Types and Sizes | 3: Get Some Input | 4: Operators | 5: Strings | 6: Conditionals | 7: Loops | 8: Methods | 9: Arrays

Photo by Yung Chang on Unsplash

It depends…only if!

To evaluate conditions Java offers the if/else if/else statement and the switch. Code along as we go through the lesson. The code is available on GitHub.

If / Else if / Else

The conditional executes a statement based on a condition. The most basic conditional only contains an if statement:
If a certain condition is true, then a block of code is executed.
Let’s create an example for better illustration. Check if it rains and make a decision if you take an umbrella on your walk or not.
boolean rain = true;
if (rain == true) {
System.out.println(“It is raining, can you bring an umbrella?”);
}
else {
System.out.println(“It is not raining, let\’s go.”);
}

This can be extended by adding an else statement, which executes another block of code if the condition of the if statement is not true.
If a certain condition is true, a block of code is executed.
Else, if the condition is not true, another block of code gets executed.

You can have several conditions implemented with additional else if statements.
If a certain condition is true, a block of code is executed.
Else if another condition is true, a block of code is executed.
Else, if no condition is true, a block of code gets executed.

To sum it up:
If (condition = true) {…code to execute}
else if (another condition is true) {…code to execute}
else {…code to execute}

Let’s code an if/else if/else statement. We ask for a user to guess our favorite number.
If the user guesses a smaller number, we output: “My favorite number is greater than your guess.”
Else, if the user’s guess is correct, we output: ”Yes, you guessed my favorite number.”
Else, we output: “My favorite number is lower than your guess.” No additional condition is necessary since no other options are possible.

In our example, we hard-coded the user’s guess, it has a value of 5.
Create an integer variable number:
int favoriteNumber = 123;
if (favoriteNumber > 5) {
System.out.println(“My favorite number is higher than your guess.“);
}
else if (favoriteNumber == 5) {
System.out.println(“Yes, you guessed my favorite number.“);
}
else {
System.out.println(“My favorite number is lower than your guess.“);
}

Ternary

An if&else statement can be written in a more concise form, as a ternary. We covered the ternary already in part 5 Operators but we repeat it since it fits right in this section.

(condition) ? expressionIfTrue : expressionIfFalse;

answer = (enoughFood == true) ? “There is enough food.” : “I am hungry.”;

If you have more than 3 conditions, it’s worthwhile considering the switch statement. It works the same as the if/else if/else statement.

Switch

A switch is basically a list of conditions, cases, you run through. Once the case that evaluates to true is found, the switch terminates through the break keyword. The switch can have a default case, for the case that no condition is true. The default case must no have a break.
You provide a parameter to the switch method. The switch will check for the value that sets that condition true and will output it accordingly.
In our example, we search for the day of the week. We create a variable dayOfTheWeek that holds today’s data as an integer. Then we create cases 1–7, which will output the corresponding day, which we save in the variable day. For the case that the provided day number is outside of the integer range 1–7, we create a default case with the message: “No day selected.”

int dayOfTheWeek = 0;
String day;

switch(dayOfTheWeek) {
case 1:
day = “Monday”;
break;
case 2:
day = “Tuesday”;
break;
case 3:
day = “Wednesday”;
break;
case 4:
day = “Thursday”;
break;
case 5:
day = “Friday”;
break;
case 6:
day = “Saturday”;
break;
case 7:
day = “Sunday”;
break;
default:
day = “No day selected”;
}

Get the code for this lesson at GitHub.

In the next lesson, we are going to talk about Loops.

--

--