Switch Statements in Java




Welcome to the switch statements tutorial. Today, we’ll study how to switch from one option to other, when you are given several number of options.

Everything that is done in here can also be implemented using if statements that we have learnt in previous tutorial. But using of switch statements are more neat in some situations.

Format of switch statements (we’ll discuss this with an example):

switch (<expression>) {

case <constant-1> :

               <statements-1 >

                break;

case <constant-2 > :

               <statements-2>

                break;

.
.
.

case <constant-N> :

               <statements-N>

               break;

default:

               <statements-(N+1)>

 }

OK. So, consider this case. There is a certain technical course conducted for a group of students but in order to apply for it, all of them have to face an exam first.

The results in the exam are given according to a grading format. They are as follows.

  • Grade A – Marks are between 100 and 75.
  • Grade B –  Marks are between 75 to 50.
  • Grade C –  Marks are between 50 to 35.
  • Grade F –  Marks are less than 35.

If you have got “F”, then you can’t enroll into the course. How do we write a program so that if some person enters his or her grade into it, then these info is given out?

For this case, switch statements are the programmer’s choice 🙂 Let’s see how do we write it…

Now let’s observe this carefully.

The expression within brackets of the switch statements is the variable grade.

So we consider each case of this grade variable. If the grade has the String value “A” in it, then the statements under case “A” are executed.

If the grade is not the case “A”, then the statements below it, are skipped and computer moves to next case.

Likewise, for case “B”, “C” and “F” same process is applied and if the value in grade is none of the cases “A”, “B”, “C” or “F”, then the statements under default are executed.

The break keyword is a special one. It is used to get out of the switch statement block.

For example, if you got a grade “B”, then once the statements under caseC” finished the execution, there is no need to move to the next caseC” and waste your time.

Therefore, all you need to do is to jump out of the switch statement and that is done by break.

But what happens if you just didn’t write it?? Then, after the execution of case “B” statements, the program will consider the next caseC” as well.

Final note: default is not always compulsory to write. Try deleting the default case.

But if you give a wrong input other than the acceptable ones, your program is not capable of identifying that error. It will just exit the program.

So here’s the complete code for the previous example where you are allowed to input the grade while running the program using the Scanner input. Note that I have used grade.toUpperCase(); to make the user input always capital when processing the switch statement so even if you write “a” instead of “A” for the grade, it responds correctly.