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

please explain which output is right?

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


This question is from enthuware.If we execute this program it gives the output 1,2,3,4 and if we comment the line (1) then it gives a different output as 11121314212223243132333441424344...

My question is the above output must be at one time when line is not commented...
Why does program in output 1234 does not print j it only prints i.It never reaches line 2 ...This is what I dont understand why??
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the above code we have "continue loop" right that is the reason we get 1,2,3,4. I will explain how it is so.

Here loop is a label. Initially when the control comes to first for loop (i.e i for loop here in our case) it has the i value as 1 and then it enters into the j for loop then it prints the i value i.e 1 to the console and then comes to the if statement here x equals 0 condition gets satisfied and hence continue loop statement gets executed. Now the tricky thing happens, the control immediately jumps out of the j for loop and then goes back to the label loop: part and then it increments the i value to 2 now and then checks the condition whether it is lesser than 5 or not and if so it enters into the j for loop again and now this time it prints 2 to the console and in the same fashion the rest of the output 3,4 is displayed on the console.

If we comment line 1 then the control comes to j print statements also and hence j values will also get printed.

I hope my explanation made you clear if not feel free to ask

Regards
Vijay
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Vijay,

the line does not ever reach line 2 I mean this is what I dont understand why does it not reach line 2...as far the brackets of IF statement is concerned it also is closed prior to that..still it does not reach line 1 I mean why???
 
S. Vijay
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You try to go through working of continue.

Yes the control does not reach line 2 because of the contine statement. Always the value of x is 0 and the condition gets satisfied every time and on encountering continue loop it jumps out of the j loop before printing the j value and the control comes to the i for loop. Does my explanation clear your doubt?
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

Thanks I think now I understand it now....
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think Sonali needs to do some reading about continue and break in a loop and what each of them does then you will figure out why line 2 was never reached.
Note that x=0 has never changed in your code and there is no modification or increment for this value thus condition is always true and continue is always executed.

Cheers!!!
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Sonali
See JLS for continue operation
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic