Hello Everyone!
Back to our Java tutorial we have discussed Java if-else statements. Today the topic of our tutorial is Switch statement in Java. We will learn how to do use switch statements. Where we should use the If-else statement and where the Switch Statment. So basically A switch statement allows a variable to be tested for equality against a list of values. The values used in the switch is called the case.
The variable passed through Switch will be checked with each case. If the variable match with the case the compiler will execute the particular block of code and will leave the rest of the cases if the break statement is found and if there is no break statement in the switch the compiler will execute the whole cases. The switch statement work without a break statement but a break statement is recommended to execute the specific block of code.
Syntax
switch(expression) {
case value :
// Statements
break;
case value :
// Statements
break;
// You can have any number of case statements.
default :
// Statements
}
The Switch statement should contain a default case. Basically what default case does is if no case matches with the given expression the compiler will execute the default case.
To understand the concept and the function of the Switch statement I have written a very basic code.
String name ="pakgamer";
switch (name){
case "pakgamer":
System.out.println("Yes! The name of the user is "+name);
break;
case "hive":
System.out.println("Yes! The blockchain name is"+name);
break;
case "steemit":
System.out.println("The blockchain name is "+name);
break;
default:
System.out.println("Oh! You have entered wrong name");
}
First of all, I have created a variable with my username assigned. Then I have the switch statement with 3 cases and a default case to check if the name match with the given cases if so then execute the block of code inside it and if not then the execute the code in the default case. If I run this code it will print the first case because the given String data match with the string inside the first case.
As you can see the code in the first case is executed. Now to check and understand better let's change the name value to the hive.
And you can see the second case is matched and the code is executed now if I put something that is not in the switch cases then what will happens? Let's find out
And if you noticed the default case is executed. One more thing I want to add here is the case value of two cases can't be the same. Then the compiler will not able to decide which case should be executed.
As you can see the compiler is telling me that you should remove the duplicate case before compiling.
If I remove the break statement let's see what happens
Even the first case is matched still the whole cases long with default cases are executed.
If the default case is removed and no case match then what will happen?
Nothing will execute because there is no default case and the given data does not match with the following cases.
One more thing if you have noticed is that the break and default case is optional. If they don't exist the program will still work but may not fulfill your task.
Thank you for reading.
To become a successful developer you need a lot of experience, which unfortunately I don't have yet. I just started training and the main source of help for me is data mining assignment help where I get all the necessary information. Which will soon help me to become an experienced specialist.