Syntax : Switch Case in Java Programming
- It is alternative to else-if ladder.
- Switch Case Syntax is similar to – C/C++ Switch.
- Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression.
- The expression used in the switch statement must return an int, a String, or an enumerated value.
switch (selection) { // value
case value1 : // checking value 1
statement(s);
break; // use to break switch flow if condition match
case value2 : // checking value 2
statement(s);
break;
.
.
case value_n :
statement(s);
break;
default:
statement(s);
}
Different Ways of Using Switch Case :
Switch Case Using Integer Case
int i=3;
switch (i) {
case 1 :
System.out.println("One player is playing this game.");
break;
case 2 :
System.out.println("Two players are playing this game.");
break;
case 3 :
System.out.println("Three players are playing this game.");
break;
default:
System.out.println("You did not enter a valid value.");
}
OutPut :
Three players are playing this game.
Switch Using String
String name = "Zubair";
switch (name) {
case "ALI" :
System.out.println("ALI is playing this game.");
break;
case "Zubair" :
System.out.println("Zubair is playing this game.");
break;
case "Ubaid" :
System.out.println("Ubaid is playing this game.");
break;
default:
System.out.println("You did not enter a valid name.");
}
OutPut :
Zubair is playing this game.
- If you are coming from C/C++ then you must understand this concept that – Java Supports String Inside Case Label.
- Above program if used in C/C++ then it will give us compile time error.
How Switch Case Statement is different in java from c programming
- In C Programming we were unable to write Case label of data type ‘String’. In java we can have String inside “Switch”.
Example : Choose Day of the Week
class SwitchDate{
public static void main(String[] args){
int week = 3;
switch (week){
case 1:
System.out.println("monday");
break;
case 2:
System.out.println("tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("thursday");
break;
case 5:
System.out.println("friday");
break;
case 6:
System.out.println("saturday");
break;
case 7:
System.out.println("sunday");
break;
default:
System.out.println("Invalid week");
break;
}
}
}
Output :
wednesday
Nicely explained. Specially those examples. I have also wrote about Switch Case Check out here
ReplyDeletematerial proved to be really efficient and beneficial to me. Thank you very much for providing this information ..visit here Power Apps development company in Canada / power apps consultation canada
ReplyDeleteThanks for this wonderful information We specialise in Websites, marketing, online business development & cloud based systems. We Help Businesses Grow through Digital Marketing and Business Development Services
ReplyDeleteThank you for sharing the article. Looking for digital transformation services?
ReplyDeleteThe data that you provided in the blog is informative and effective.I am happy to visit and read useful articles here. I hope you continue to do the sharing through the post to the reader. For more detail visit Trans Tech Solutions .
nice blog! also join us on https://it34.alhuda.com.pk/
ReplyDeleteNice Post….!!!! Greps Ai specializes in transforming businesses with cutting-edge technology solutions. Leveraging expertise in Digital Marketing, Chatbot Development, API Development, and Software Development Designing, Greps Ai empowers companies to achieve exponential growth. By integrating AI-driven strategies and innovative software, Greps Ai for Business Growth ensures scalable solutions tailored to meet diverse business needs, fostering efficiency and competitive advantage in today's dynamic market landscape.
ReplyDelete