If-then and If-Then-Else Statements in Java, also The Switch Statement
In Java, there are two selection constructs:
- The if-then statement
- The switch statement
Note: For those who are just beginning to learn the Java language, it will be enough to only learn the if-then statement, because it is used very often. The switch statement is used much less often.
To better understand what a selection construct in Java is, imagine that you are standing at a crossroads. See the picture below.
If you go left, you'll end up at the office.
If you go straight, you'll end up at the gym.
If you go right, you'll end up at the beach.
So keep in mind that the selection construct (the if-then statement) works along the same principle. In a sense, it IS a crossroads.
1 2 3 4 5 6 7 8 9 |
if (to the left){ work in the office; } else if (straightforward) { do physical exercises; } else { lay on the beach; } |
The general form of an "if" statement in Java is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
if (condition) { //actions, that must be done if the condition is true; } else if (condition) { //actions, that must be done if the condition is true; } else if (condition) { //actions, that must be done if the condition is true; } else { //actions, that must be done if the condition is true; } |
Note:
- The statement always starts with the word "if", followed by the condition in parentheses.
- A semicolon is never used after parentheses.
- The word “else” is used in order to specify an alternative option ("if it doesn’t hold true, then").
- If there are several conditions, then each of them will be written with the help of the “else-if” combination and the alternative variant will be written in parentheses after these conditions. The last option ("if, else if, else if") will be written with “else” without a condition.
Let's solve some tasks.
Task No. 1
It is necessary to write a program which will ask the user to enter the number of his/her choice (1, 2, or 3) and then the program will say which number was entered by the user: 1, 2, or 3.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class Test { public static void main (String [] args) { System.out.print ("Enter 1, 2 or 3: "); Scanner inputFigure = new Scanner (System.in); int i = inputFigure.nextInt (); if ( i==1) { System.out.println ("You entered the number 1"); } else if (i==2) { System.out.println ("You entered the number 2"); } else if (i==3) { System.out.println ("You entered the number 3"); } else { System.out.println ("You entered the number not equal to 1, 2 or 3"); } } } |
Commentary:
- The condition states that the user needs to enter the number of his/her choice: 1, 2 or 3. Therefore, the first thing we need to do is to ask the user to enter a number. So we write the following in the code:
1 |
System.out.print ("Enter 1, 2 or 3: "); |
2. Then the user needs to enter the number and the computer needs to read it out from the console. To do this, we import the scanner with the help of the following line:
1 |
import java.util.Scanner; |
- Now with the help of the scanner (specifically these two lines), we assign the number entered by the user to the variable "i":
1 2 |
Scanner inputFigure = new Scanner (System.in); int i = inputFigure.nextInt (); |
If you don’t know how the scanner works (i.e. you don’t understand these two lines), you should read the following article: "The Scanner in Java (Input and Output)."
- Then we check whether the i variable equals 1. If it equals 1, then the message “You entered the number 1” will be displayed on the screen.
1 2 3 |
if ( i==1) { System.out.println ("You entered the number 1"); } |
- If the i variable doesn’t equal 1, we check whether it equals 2. If it equals 2, then the message “You entered the number 2” will be displayed on the screen.
1 2 3 |
else if (i==2) { System.out.println ("You entered the number 2"); } |
- If the i variable doesn’t equal 1 or 2, then we check whether it equals 3. If it equals 3, then the message “You entered the number 3” will be displayed on the screen.
1 2 3 |
else if (i==3) { System.out.println ("You entered the number 3"); } |
- If it turns out that all the previous steps were "false" (i.e. the i variable doesn’t equal 1, 2, or 3), then the operator will execute the code written in “else” and the message “You entered a number which isn’t equal to 1, 2, or 3” will be displayed on the screen.
1 2 3 |
else { System.out.println ("You entered a number not equal to 1, 2, or 3"); } |
And the operator has finished. End of the program.
And once again, we'd like to point out that:
- A statement always starts with the word "if," followed by the condition in parentheses.
- A semicolon is never used after parentheses.
- The word “else” is used in order to specify an alternative option ("if it doesn’t hold true, then").
- If there are several conditions, then each of them will be written with the help of the “else-if” combination and the alternative option will be written in parentheses after these conditions. The last option ("if, else if, else if") will be written by means of “else” and will not have a condition.
- We also want to draw your attention to the fact that we used a double equals sign ("==") in "if (i==1)". It’s a common mistake for beginners to use "=" instead of "==."
Reminder:
- When we assign a value to a variable, we use =.
For example:
int i = 1;
int k =7;
- When we check whether variable "i" equals 1, we use "==." That’s why we wrote "if ( i==1)" and so on and so forth.
Task No. 2
It’s necessary to write a program which will ask the user to enter the number 1. If the user enters the number 1, the program should display the following message: “You entered the number 1.” If the user enters some other number, the program should display the following message: “You entered a number not equal to 1.”
Yes, this task is similar to the first one. You will understand why we chose this task as an example.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Scanner; class Test { public static void main (String [] args) { System.out.print ("Enter the number 1: "); Scanner inputFigure = new Scanner (System.in); int i = inputFigure.nextInt (); if ( i==1) { System.out.println ("You entered the number 1"); } else { System.out.println ("You entered a number not equal to 1"); } } } |
Commentary:
Compared to Task No. 1, this task has fewer conditions which need to be checked. Look how the "if" construction appears in this task:
1 2 3 4 5 6 |
if ( i==1) { System.out.println ("You entered the number 1"); } else { System.out.println ("You entered the number not equal to 1"); } |
We have an “if” and an “else,” but we don’t have an “else if.” Yep, the same rule applies here as well:
- A statement always starts with the word "if," followed by the condition in parentheses.
- A semicolon is never used after parentheses.
- The word “else” is used in order to specify an alternative option ("if it doesn’t hold true, then").
- If there are several conditions, then each of them will be written with the help of the “else-if” combination and the alternative option will be written in parentheses after these conditions. The last option ("if, else if, else if") will be written by means of “else” and will not have a condition.
Indeed, this construction starts with “if” and ends with “else.” In this example, there isn’t an “Else if,” because we are checking only one condition "if ( i==1)." If we were checking two or more conditions like in Task No. 1, then we would have “else if” between “if” and “else.”
Task No. 3
It is necessary to write a program which will ask the user to enter the number 1. If the user enters the number 1, the program should display the following message: “You entered the number 1.” If the user enters some other number, the program shouldn't do anything.
Yes, this task is similar to the second one; the only difference is the second part of the condition. Let's take a look at how this task is solved.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Scanner; // class Test { public static void main (String [] args) { System.out.print ("Enter the number 1: "); Scanner inputFigure = new Scanner (System.in); int i = inputFigure.nextInt (); if ( i==1) { System.out.println ("You entered the number 1"); } } } |
Commentary:
As we can see from the solution, there is an “if” but there is no “else.”
Why? Because, according to the condition of the problem, we need to display the message “You entered the number 1” if the condition is "true," but if the condition is "false," the program shouldn't do anything. That’s why we didn’t use “else” in this code.
The Switch Statement
Structures with "if else" operators that have a number of conditional branches can be very bulky. Because of this, there’s a convenient solution for situations like this where it is necessary to repeat the test of the same value of the variable - the switch statement.
Let’s take a look at how the switch statement works.
Task No. 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; // class Test { public static void main(String args[]){ System.out.println("Enter 1, 2, 3, or 4"); Scanner scan = new Scanner(System.in); int number = scan.nextInt(); switch (number){ case 1: System.out.println ("You entered the number 1"); break; case 2: System.out.println ("You entered the number 2"); break; case 3: System.out.println ("You entered the number 3"); break; case 4: System.out.println ("You entered the number 4"); break; default: System.out.println("You entered a wrong number"); } } } |
If you run this code on your computer, you will see the following in your console:
Enter 1, 2, 3, or 4
And if you enter the number 1, you will see the following in the console:
Enter 1, 2, 3, or 4
1
You entered the number 1
If you enter the number 5, you will see the following:
Enter 1, 2, 3, or 4
5
You entered a wrong number
Commentary:
First, we suggested that the user enter one of the following numbers: 1, 2, 3, or 4.
1 |
System.out.println("Enter 1, 2, 3 or 4"); |
Then we imported the scanner with the "nextInt" method for integers, because we expect the user to enter 1, 2, 3 or 4 and all of them are integers.
Then we checked the value of the "number" variable:
1 |
switch (number){ |
We specified all the possible values for the variable "number" as well as the code that will be executed in the event of this or that number:
1 2 3 4 |
case 1: System.out.println ("You entered the number 1"); break; case 2: System.out.println ("You entered the number 2"); break; case 3: System.out.println ("You entered the number 3"); break; case 4: System.out.println ("You entered the number 4"); break; |
If the user enters an integer which isn’t equal to 1, 2, 3, or 4 (in our example, we supposed that a user might enter 5), then the following line of the code will be activated:
1 |
default: System.out.println("You entered the wrong number"); |
Note: each "case" must be closed by means of "break". "Break" allows the program to stop executing the code and exits the operating condition.
The general form of a switch statement is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch (a variable to be checked){ case value1: code-to-be-run-if-the-variable==value1; break; case value2: code-to-be-run-if-the-variable==value2; break; case value3: code-to-be-run-if-the-variable==value3; break; default: code-to-be-run-if-the-value-is-not-set-in-case; } |
Let’s summarize:
In Java there are two so-called selection constructs:
- The if-then statement
- The switch statement
The general form of "if" statement in Java is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
if (condition) { //actions, that must be done if the condition is true; } else if (condition) { //actions, that must be done if the condition is true; } else if (condition) { //actions, that must be done if the condition is true; } else { //actions, that must be done if the condition is true; } |
It may also be in the following form:
1 2 3 4 5 6 |
if (condition) { //actions, that must be done if the condition is true; } else (condition) { //actions, that must be done if the condition is true; } |
Or:
1 2 3 |
if (condition) { //actions, that must be done if the condition is true; } |
The general form of a switch statement is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch (a variable to be checked){ case value1: code-to-be-run-if-the-variable==value1; break; case value2: code-to-be-run-if-the-variable==value2; break; case value3: code-to-be-run-if-the-variable==value3; break; default: code-to-be-run-if-the-value-is-not-set-in-case; } |