Java Switch - Case statement is almost same as the if -- else statement.
Let us understand Java Switch - Case with the below example.
Say you are a class teacher and there are only 5 students in your class.
And you have marked them with their Roll Numbers and their Names.
Now, say the Principal of the school has asked you to write a Java program, that will show you the name of a student once you enter his/her roll number.
Now, that you are an expert in if --- else. You wrote the program using if --- else.
public class MyApplication { public static void main(String[] args) { int rollNumber = 2; if (rollNumber == 1) { System.out.println("Ronald"); } else if (rollNumber == 2) { System.out.println("John"); } else if (rollNumber == 3) { System.out.println("Murali"); } else if (rollNumber == 4) { System.out.println("Satish"); } else if (rollNumber == 5) { System.out.println("Debasish"); } else { System.out.println("The student does not exist."); } } }
So, you wanted to search the name of the student whose roll number is 2.
And you got the output as John.
Now, just think for a moment. What if there were 100 students in your class? You had to write 100 if -- else -- if statements.
Well! The good news is Java provides a replacement for the above case. Where the same variable needs to be compared with several values.
What I meant is, the same variable rollNumber is repeated at every if -- else -- if statements.
And to avoid this repetition, Java switch - case comes to rescue.
So, at first, let us rewrite the above program using Java switch - case. Then we will understand how it works?
public class MyApplication { public static void main(String[] args) { int rollNumber = 2; switch (rollNumber) { case 1: System.out.println("Ronald"); break; case 2: System.out.println("John"); break; case 3: System.out.println("Murali"); break; case 4: System.out.println("Satish"); break; case 5: System.out.println("Debasish"); break; default: System.out.println("The student does not exist."); break; } } }
Now, if you look at the current code. It is quite cleaner, compared to the if -- else -- if code.
int rollNumber = 2;
There is a switch statement, where we specify the rollNumber.
switch (rollNumber)
Then there are cases, like case 1, case 2, e.t.c.
case 1: System.out.println("Ronald"); break; case 2: System.out.println("John"); break; case 3: System.out.println("Murali"); break; case 4: System.out.println("Satish"); break; case 5: System.out.println("Debasish"); break; default: System.out.println("The student does not exist."); break;
And for every case, it checks for the rollNumber(2 in this case as rollNumber=2).
When it finds a match in case 2.
case 2: System.out.println("John"); break;
It print "John" and comes out of the switch statement as we have specified a break statement.
switch(expression/variable) { case value1: // code for value1 break; case value2: // code for value2 break; default: // code for default block break; }
The default block is also an optional block,
default: System.out.println("The student does not exist."); break;
The default block is only executed none of the cases match. And is exactly same as,
else { System.out.println("The student does not exist."); }
For the if -- else -- if code.
public class MyApplication { public static void main(String[] args) { int rollNumber = 10; switch (rollNumber) { case 1: System.out.println("Ronald"); break; case 2: System.out.println("John"); break; case 3: System.out.println("Murali"); break; case 4: System.out.println("Satish"); break; case 5: System.out.println("Debasish"); break; default: System.out.println("The student does not exist."); break; } } }
In the above code, we are checking for the student whose roll number is 10. And since roll number 10 does not exist, the default block is executed.