• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch's default - unexpected behavior

 
Bronson King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and in EXAMPLE 2 how can you tell that default case had actually run???
 
Bronson King
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic