The if-then and if-then-else Statements in Java. The switch Statement

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

In Java, there are two so-called 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 learn only 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 to left, you'll end up at the office.

If you go forward, you end up at the gym.

If you go right, you'll end up at the beach.

Well, you should remember that the selection construct (if-then statement) works along the same principle. In some ways, it IS the 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 round brackets.
  • A semicolon is never used after the round brackets.
  • 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 round brackets after these conditions. The last option ("if, else if, else if") will be written with “else” without a condition.

We suggest that you solve the following tasks with us.

Task No. 1

It is necessary to write a program which will ask a user to enter a 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 need to enter the number of his, choice: 1, 2 or 3. Therefore, the first thing we need to do is to ask the user to enter the number. So we write the following in the code:

2. Then the user needs to enter the number and we need to read it out from the console. For this reason, we import the scanner with the help of the following line:

  1. Now with the help of the scanner (namely 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 is done. End of the program.

And, once more, we draw your attention to the fact that:

  • The statement always starts with the word "if", followed by the condition in round brackets.
  • A semicolon is never used after the round brackets.
  • 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 round brackets after these conditions. The last option ("if, else if, else if") will be written by means of “else” without condition.
  1. We also want to draw your attention to the fact that we used the double equal 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 in the code.

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. Now you’ll understand why we have used this task as an example.

Solution:

Commentary:

Compared to Task No. 1, this task has fewer conditions which need to be checked. Let’s take a look at how the "if" construction looks in this task:

We only have “if” and “else”, but we don’t have an “else if”. Yep, the same rule applies here as well:

  • The statement always starts with the word "if", followed by the condition in round brackets.
  • A semicolon is never used after the round brackets.
  • 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 round brackets after these conditions. The last option ("if, else if, else if") will be written with “else” without a condition.

Indeed, this construction starts with “if” and ends with “else”. In this example, there isn’t any “Else if”, because we check only one condition "if ( i==1)". If we had checked two or more conditions like in Task No. 1, then we would have had “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, the program is similar to the second one, the only difference being the second part of the condition. Take a look how this task is solved.

Solution:

Commentary:

As we can see from the solution, there is “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 more elegant solution for situations 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, for example, the number 1, you will see the following in the console:

Enter 1, 2, 3, or 4

1

You entered the number 1

For example, if we enter the number 5, then 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 the numbers 1, 2, 3, or 4:

Then we imported the scanner with the "nextInt" method for integers, because we expect the user to enter the numbers 1, 2, 3 or 4, all of which are integers.

Then we checked the value of the variable "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:

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:

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Leave a Reply

Your email address will not be published.

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