If-then and If-Then-Else Statements in Java, also The Switch Statement

FacebooktwittertumblrFacebooktwittertumblr

In Java, there are two selection constructs:

  1. The if-then statement
  2. 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_Vertex Academy

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.

The general form of an "if" statement in Java is as follows:

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:

Commentary:

  1. 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:

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. Now with the help of the scanner (specifically these two lines), we assign the number entered by the user to the variable "i":

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)."

  1. 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. 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. 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. 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.

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.
  1. 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:

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:

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:

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

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.

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:

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:

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:

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:


Let’s summarize:

In Java there are two so-called selection constructs:

  1. The if-then statement
  2. The switch statement

The general form of "if" statement in Java is as follows:

It may also be in the following form:

Or:

The general form of a switch statement is as follows:

 

FacebooktwittertumblrFacebooktwittertumblr

FacebooktwittertumblrFacebooktwittertumblr
Самоучители--узнать детальнее--