Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch stmt

 
E Weibust
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One think about preparing for the certification exam. It has got me "nit-picking" everything about the java programming language. May be I should just say, "It works that way because the inventors wanted it to work that way", but I don't. What I do is ask people why, or post to places like this. Sorry about that rant......
My latest "why" question. Why does the switch stmt skip the body of case arguments until it "matches" the switch condition and then execute code in all case bodies' until the end of the loop. See example:
char c = 1;
switch (c) {
default:
System.out.println("case default");
case 1:
System.out.println("case 1");
case 2:
System.out.println("case 2");
case 3:
System.out.println("case 3");
} // end switch
In the code above the default case is skipped, but case 1-3 is executed. What is the point of skipping until a hit, but then executing all the remaining cases? I know the break can be used, but it still bothers me.
Thanks,
Erik
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by E Weibust:
What is the point of skipping until a hit, but then executing all the remaining cases? I know the break can be used, but it still bothers me.
Thanks,
Erik

Blame it on C and its derivative C++.
I guess the developers of Java want to retain some of the popular aspects of C, which is a popular language at that time. If you were to compare the two, you will notice a number of similarities, such as: if, while, do, for, break statements, the use of curly braces, the method declaration, arrays, and many more.
[ June 19, 2003: Message edited by: Alton Hernandez ]
[ June 19, 2003: Message edited by: Alton Hernandez ]
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understood correctly, the default case is first, but of course it is not executed unless the other cases match. Actually, I find that a little confusing, because you can traverse all the cases and if no match occurs, then you jump up the the default statement at the top and then go down again. Better to put the default case at the bottom in practice.
The advantage of having it "fall through" let's you have one piece of code work for multiple cases by utiliziation the fall-through behavior.
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it that way:
Suppose you are testing your switch for a variable age.
If age is 18 something happens. If age is 21, something else happens PLUS what happens for age = 21. What would you do ? If it werent for the fall thru schema, you would have to repeat the code inside case 18 also for the case 21 right ? Adding the "must insert break " rule, you can create a case 21 doing its unique stuff and not add a break, and then include the case 18 stuff with a break. When case is 18 only the case 18 code will be executed. When age is 21, both pieces of code will be executed.
Hope that helps,
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also gives you the ability to do this:
switch (i) {
case 1:
case 2:
case 3:
case 4: //do something then break
case 5:
case 6:
case 7:
case 8: // do something else then break
 
E Weibust
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Fair enough. What I take from all of this is the main benefit of the "fall through" feature is that you can run multiple case blocks when your expression equals only one case.
Thanks...
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ June 19, 2003: Message edited by: leo donahue ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic