aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch's default - unexpected behavior 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 Watch "switch New topic
Author

switch's default - unexpected behavior

Bronson King
Greenhorn

Joined: May 25, 2008
Posts: 9
I know all the rules about the switch that I'm supposed to but there is one thing that is bugging me. My compiler tells me it works this way, just trying to understand why.

So we all know that if you don't break a result that it just becomes the entry point and falls through running every remaining case's expression until it ends or breaks. This implies that the whole thing works sequentially, top down. However I am thrown...

// EXAMPLE 1:
char toLookFor = 'c';
switch (toLookFor) {
case 'a': System.out.print("a");
default: System.out.print("X");
case 'b': System.out.print("b");

This returns "Xb" as I would expect.

But if I change the charToLookFor to 'b'...

// EXAMPLE 2:
char toLookFor = 'b';
switch (toLookFor) {
case 'a': System.out.print("a");
default: System.out.print("X");
case 'b': System.out.print("b");

Then the output is just 'b'.

Does this mean that, even though the rule is "a default doesn't have to be at the end" it still gets run at the end??? It either knows it has a match and skips it or ignores the default until the end???


~B King~
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
and in EXAMPLE 2 how can you tell that default case had actually run???


<a href="http://faq.javaranch.com/java/UseRealWords" target="_blank" rel="nofollow">Use Real Words</a> <a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">!!!Use Code Tags!!!</a> <a href="http://faq.javaranch.com/java/SayThanks" target="_blank" rel="nofollow">Say Thanks</a><br />scjp6
Bronson King
Greenhorn

Joined: May 25, 2008
Posts: 9
Originally posted by Ivan Ivanic:
and in EXAMPLE 2 how can you tell that default case had actually run???


That's what I'm saying ... it must not of ... which is weird to me. You'd think with something called fall-through, order would part of it's thing. Guess not.
Bronson King
Greenhorn

Joined: May 25, 2008
Posts: 9
And what is also strange is, its such a big deal that "default doesn't have to be last" when really its placing only matters if its caught in a fall-through ... it runs "last" anyways.
Ivan Ivanic
Ranch Hand

Joined: Oct 31, 2007
Posts: 100
Originally posted by Bronson King:

Does this mean that, even though the rule is "a default doesn't have to be at the end" it still gets run at the end???


It gets run if there is no matching case.
It will run all that is under it if it don't have break, until next break if any.

char toLookFor = 'b';
switch (toLookFor) {
case 'a': System.out.print("a");
default: System.out.print("X");
case 'b': System.out.print("b");

Then the output is just 'b'.


and it should be like that. It got matching case 'b' case, which is in the end so only 'b' case got run.
Bronson King
Greenhorn

Joined: May 25, 2008
Posts: 9
Okay, I am clear on this. I was to start, just seems odd to me not going in order, so talking about its oddness will help me remember. Thanks.
Nareshreddy Kola
Greenhorn

Joined: May 24, 2008
Posts: 6
So when there is no match and default case is present, then it will print all sop statements starting from default case, if not, only the matching case sop will be printed.

Thanks King and Ivan.


Kola.Nareshreddy
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Originally posted by Nareshreddy Kola:
So when there is no match and default case is present, then it will print all sop statements starting from default case, if not, only the matching case sop will be printed.

Thanks King and Ivan.
No, as Bronson King pointed out, the "breaks" are missed out. The switch will start from whichever case it matches, then keeps going until it reaches the end of the block (or, in a correctly written "switch-case") a "break;".
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

There a fall through for program 1.

For program 2 there is a fall through as well, but the 'b' is executed and you dont feel the effect of the fall through


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

Originally posted by Bronson King:


That's what I'm saying ... it must not of ... which is weird to me. You'd think with something called fall-through, order would part of it's thing.

I guess it depends on your definition of 'order'. it appears to me the order DOES matter. execution jumps in to whatever case matches, and proceeds. maybe you want to write code where the 'default' is you do EVERYTHING, and each individual case is leaving something off...


maybe i had a cake that was already baked, and all i need to to is frost it and pack it up. USUALLY i bake my own cakes, but this time i didn't have to. I wouldn't want it to go into the default case every time.

think of the 'switch' lines as nothing more than a point of entry, but beyond that, they aren't significant. once you're in, they are ignored. 'default' is no different than any of the others.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch's default - unexpected behavior