
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…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
String grade = "B"; switch(grade){ case "A": System.out.println("Congratulations! Your marks is " + "in the range 100 - 75"); break; case "B": System.out.println("Your marks is in the range 50 - 75"); break; case "C": System.out.println("Your marks is in the range 35 - 50"); break; case "F": System.out.println("Sorry. Your score is not " + "enough to follow the course"); break; default: System.out.println("Error!"); System.out.println("The grade you entered is not valid!"); } |
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 case “C” finished the execution, there is no need to move to the next case “C” 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 case “C” 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; public class SwitchStatements{ public static void main(String[] args) { Scanner userInput = new Scanner(System.in); System.out.print("Please enter the grade : "); String grade = userInput.next(); grade = grade.toUpperCase(); switch (grade) { case "A": System.out.println("Congratulations! Your marks is " + "in the range 100 - 75"); break; case "B": System.out.println("Your marks is in the range 50 - 75"); break; case "C": System.out.println("Your marks is in the range 35 - 50"); break; case "F": System.out.println("Sorry. Your score is not " + "enough to follow the course"); break; default: System.out.println("Error!"); System.out.println("The grade you entered is not valid"); } } } |