• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Break and continue inside the case statement

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The output is 241.But I am getting the answer 151.Please help me for the answer 241 since I have spend myself on this problem for 2 days and couldn�t figure out the correct answer.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding some of println statement may help you to understand why.

[ September 22, 2006: Message edited by: wise owen ]
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Shiva,


FIRST ITERATION:

h=0:

i=1
j=0;
k=h+i+j
=1


So switch(1) which results in calling of case 1:
continue label2 - will result in execution of while condition again NOT the do one
so ++j means j=1;

Then the do condition...
i=2
k=h+i+j
= 0+2+1
=3.

now case 3:
break label2; -- break out of the while loop and to the next iteration of for loop.


Second Iteration:

h=1... I think you should figure out from here what happens.

Hope this will help.
Regards,
Joshua
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have figured out where I was going wrong and this time of my hand calculation got the correct answer with the help of wise and Joshua. Thank you very much. I wouldn�t have figured it out without your help. Thanks again for your time. Great work.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva,

I think I can guess where your frown came from.

These labels can be tricky, especially in a do-while loop.
Where does the "continue" jump in this example?


It's tempting to say: "Well, it jumps to the label, and the next line is do, followed by if (i==2)."
But that's wrong.
The continue jumps to the while and the i is incremented.
If it wouldn't, the example would be an endless loop. But it isn't, it will print out

013READY


In the example the same would happen if you said continue; (without label).



Yours,

Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic