• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

What is this?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody.
Consider this code:
public class Test {
public static void main(String st[]){
l1:
for(int i=4; i<=7; i++)
switch(i){
default:{System.out.println("default");}
case 4: {System.out.println("case 4");}
break;
case 5: {System.out.println("case 5");} break l1;
case 6: {System.out.println("case 6");}
case 7: {System.out.println("case 7");}
}
}
}
The above code gives the output :
case 4
case 5
However, according to me case 6 should also be displayed. And if default clause is the first statement in the Switch statement, why it is not executed. Could you please tell me the reason?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
God thats ugly code - must be a test question I assume (<aside>it always annoys me when test setter use such unrealistic examples</aside> ) . The break statement for case 5 breaks to the label "l1" (see how it has "break l1" rqather than just "break"), which is outside the for loop. So rather than continuing the loop, it steps out of the loop. Labels are very ugly and make debugging/maintaining harder. My advice is not to use them unless some performance criteria absolutely requires them.
[ June 11, 2004: Message edited by: Paul Sturrock ]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this confused me for a moment as well. i was parsing the "break l1" as a sort of a "goto l1" statement, but that's not what it does.

the l1 labels the for-loop, or gives it a name. the break l1 statement that you reach after printing "case 5" basically says "break out of the loop labeled with the string l1". so we do just that. we leave the for loop entirely, immediatly, without question. after hitting the break l1, you jump down to the first executable line after the loop, which in this case is nothing.
 
Squanch that. And squanch this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic