• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

default statement

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit quizzed about the 'default' statement
why doesn't it get printed when used like :-



TIA
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few notes about default, and break:

1. default can occur in lieu of any "case label" in the switch/case statements. It doesn't have to be last.
2. In a switch statement, the value of the switch expression is calculated, and control goes directly to the corresponding label without executing any other code. Code associated with that label begins execution.
3. Execution of code continues until a break is encountered or end of the switch block is encountered.

So if there is code associated with a case label and that code doesn't end with a break, the system keeps executing the next case's code (or the default code) until it sees a break, or the end of the block, whichever comes first.

So it may execute one case's code, then the default and whatever is after that until it sees a break.

In the first example, k was 10 so control immediately went to the label associated with 10 (case 10). All the rest of the code in the block was executed because there was no break at all. (It executes regardless of other case labels).

In the second example, control went to label 10, and executed its associated code. There was no break, so execution continued with code that was normally associated with 20, and the default.
[ October 21, 2004: Message edited by: Louie van Bommel ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Netty,
The switch statement always looks for the matching case constants. If there is a matching case constant, then that would be the first statement that gets executed. After that the execution of switch statement falls through the labels. That is all the statements after the matching case are executed. If there is no matching case constant, then it looks for default label and all statements after default label are executed.

In first case since k=10 and there are no break statements after each case, it prints 10,20.

In second case , it prints 10,20,default since default case is after case 10.

Try the following and you would see that default is printed first.
 
Kali Praveen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Didnt see Louie's post.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great Stuff Louie and Kali !
the 'TC' class name in your example says it all.. [Top Cat !]

Thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic