wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch stmt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch stmt" Watch "switch stmt" New topic

switch stmt

E Weibust
Ranch Hand

Joined: Jun 13, 2003
Posts: 54
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) {
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.

---<br />Erik Weibust<br /><a href="http://erik.weibust.net" target="_blank" rel="nofollow">http://erik.weibust.net</a>
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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.

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

Joined: May 16, 2003
Posts: 160
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

Joined: Apr 20, 2003
Posts: 168
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,

Giselle Dazzi<br />SCJP 1.4
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
E Weibust
Ranch Hand

Joined: Jun 13, 2003
Posts: 54
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.
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327

[ June 19, 2003: Message edited by: leo donahue ]

Thanks, leo
I agree. Here's the link: http://aspose.com/file-tools
subject: switch stmt
Similar Threads
Keep getting this error and not sure why
Switch statement
switch's default - unexpected behavior
switch doubt..
[HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done