This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

switch statement example

 
Greenhorn
Posts: 16
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



unable to figure out the output of the preceding code from k&b book page no-415.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Ebb is loaded - static variable x is initialized to 7. static intializer block runs , x is incremented to 8
main method begins:
for loop:
1st iteration: x becomes 9, switch goes to case 9, prints 9, fall-thru to 10, prints 10 and break
2nd iteration: x becomes 10, switch goes to case 10, prints 10 and break
3rd iteration: x becomes 11, switch goes to default, prints d, fall-thru to 13, prints 13

You get "9 10 10 d 13"

Option D is correct



Peter Swiss
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
notable point here is that the break inside switch dont interrupt the for loop; only break outside the switch has effect on for loop.
 
Gaurav Gulanjkar
Greenhorn
Posts: 16
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but how is x incremented to 9 as the operator used is post increment operator and it says that first use and then increment at first iteration.
 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurav Gulanjkar wrote: it says that first use and then increment at first iteration.



the statement is x++;

you use x then increment. The scope of "use" is only for the current statement.
So you use it - it is 8
you increment it - it is 9
 
Gaurav Gulanjkar
Greenhorn
Posts: 16
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.
thank you to all of you for clearing my doubt's.
 
Don't play dumb with me! But you can try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic