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

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

In Java there are so-called selection constructs:

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

Note: For those who just begin to learn the Java language, it will be enough to learn the if-then statement, because it is used very often. The switch statement is used much more rarely.

To better understand what the selection construct in Java is, imagine that you are staying at the crossways. See the picture below.

if_Vertex Academy

If you go to the left – you get into the office

If you go forward – you get into the gym

If you go to the right – you get to the beach

Well, you should remember that the selection construct (if-then statement) works the same way – in some way it is the crossways.

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

Note:

  • The statement always starts with the word if, followed by the condition in round brackets.
  • The semicolon is never used after the round brackets.
  • The word “else” is used in order to specify an alternative variant (if it doesn’t hold true, then).
  • If there are several conditions, then each of them will be written with the help of “else-if” combination. The alternative variant is written in the round brackets after these conditions. The last variant (if, else if, else if) is written by means of “else” without condition.

We suggest you to solve the following problems with us.

Task No.1

It is necessary to write a program which would ask a user to enter a number at his/her choice: 1,2 or 3. And the program need to say which number was entered by the user: 1, 2 or 3.

Solution:

Comments:

  1. The condition states that the user need to enter the number at choice: 1, 2 or 3. Respectively the first thing we need to do is to ask the user to enter the number. Therefore, we write the following in the code:

2. Then the user need to enter the number and we need to read it out from the console. For this reason, we will 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 – “Working with scanner in Java (input and output)”.

  1. Then we check whether the variable i equals 1. If it is equal to 1, then the massage “You entered the number 2” will be displayed at the screen

  1. If the variable i doesn’t equal 1, we check whether it equals 2. If it is equal to 2, then the massage “You entered the number 2” will be displayed at the screen

  1. If the variable i doesn’t equal 1 or 2, we check whether it equals 3. If it is equal to 3, then the massage “You entered the number 3” will be displayed at the screen

  1. If it turns out, that all the previous steps were false (i.e. the variable i doesn’t equal 1, 2 or 3), then the operator will pass on to the execution of the code written in “else” and the massage “You entered the number which isn’t equal to 1,2 or 3” will be displayed at the screen

And the operator is completed. The end of the program.

One more time, we draw your attention to the fact that:

  • The statement always starts with the word if, followed by the condition in round brackets.
  • The semicolon is never used after the round brackets.
  • The word “else” is used in order to specify an alternative variant (if it doesn’t hold true, then).
  • If there are several conditions, then each of them will be written with the help of “else-if” combination. The alternative variant is written in the round brackets after these conditions. The last variant (if, else if, else if) is written by means of “else” without condition.

You could already learn it by the example of the solved Problem No.1

  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 the value of the variable for equality, for example, whether the variable I is equal to 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 would ask a user to enter the number 1. If the user enters the number 1, the program need to display the following message: “You entered the number 1”. If the user enters some different number, the program need to display the following massage: “You entered the number which isn’t equal to 1”.

Yes, the program is similar to the first one. Now you’ll understand why we have used this problem as the example.

Solution:

Comments:

In this problem there are much less conditions which need to be checked (in comparison to the Task No.1). Let’s take a look at how the if construction looks in this problem:

We have several “if” and “else”, but we don’t have any “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.
  • The semicolon is never used after the round brackets.
  • The word “else” is used in order to specify an alternative variant (if it doesn’t hold true, then).
  • If there are several conditions, then each of them will be written with the help of “else-if” combination. The alternative variant is written in the round brackets after these conditions. The last variant (if, else if, else if) is written by means of “else” without condition.

Indeed, the 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 would check two or more conditions as it is in the Task No.1, then we would have “else if” between “if” and “else”.

Task No.3

It is necessary to write a program which would ask a user the enter the number 1. If the user enters the number 1, the program need to display the following message: “You entered the number 1”. If the user enters some different number, the program doesn’t need to do anything.

Yes, the program is similar to the second one, the only difference is in the second part of the condition. We suggest you to take a look at how this problem would be solved.

Solution:

Comments:

As we can see from the condition there is “if” but there is no “else”.

How is it? By the condition of the problem we needed to display the massage “You entered the number 1” if the condition is true, and if the condition is false then the program doesn’t need to do anything. That’s why we didn’t use “else” in this code.

The switch statement

Structures with if-else operators, which offer a number of conditional branches, could be very bulky. Therefore, there’s a more elegant solution for cases where it is necessary to repeat the test of the same value of the variable – the switch operator.

Let’s take a look at how the switch operator works.

Task No.4

If you try to run this code on your computer, you will see the following in your console:

Enter 1,2,3 or 4

Then if, for example, 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

For example, if we enter the number 5, then you will see the following:

Enter 1,2,3 or 4

5

You entered the wrong number

Comments:

First we offered the user to enter the numbers 1,2,3 or 4

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

Now we check the value of the variable number.

In order to do it we specify all the possible values for the variable number and the code that will be executed with one or another value

If the user enters  the integer which isn’t equal to 1,2,3 or 4 (in our example we assumed that the user would enter 5), then the following line of the code will be activated:

Nota bene: each case must be closed by means of break. Thanks to the break the program stops executing the code and logs out of the condition operator.

The general form of switch statement is as follows:


Let’s summarize:

In Java there are 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 switch statement is as follows:

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Leave a Reply

Your email address will not be published.

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

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