• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

loop

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ranchers,
i came through this question in dan's latest exam,
how the loop works, explanation that dan gave is not suffecient for me, anybody could reply my question?


[ Jess added UBB [code] tags to preserve whitespace -- check 'em out! ]
[ November 19, 2002: Message edited by: Jessica Sant ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it help if you break things up with a lil bit of formatting and whitespace?

step through it line by line as if you were the JVM.
*Remember that in a while statement -- you first run the test -- then execute the code in the middle.
With a do while you first execute the statement in the middle -- then run the test.*
outer loop test i++<3 TRUE (i=1)
prints j=0
inner loop test j++<3 TRUE (j=1)
prints j=1
inner loop test j++<3 TRUE (j=2)
prints j=2
inner loop test j++<3 TRUE (j=3)
prints j=3
inner loop test j++<3 FALSE(j=4)
outer loop test i++<3 TRUE (i=2)
prints j=4
inner loop test j++<3 FALSE(j=5)
outer loop test i++<3 TRUE (i=3)
prints j=5
inner loop test j++<3 FALSE(j=6)
outer loop test i++<3 FALSE(i=4)
END
.... its kinda like some country song I heard -- sometimes you're the windshield, sometimes you're the bug. With the SCPJ test -- sometimes you have to be the compiler, sometimes the JVM. (ok... bad anaolgy -- but at least I tried)
[ November 19, 2002: Message edited by: Jessica Sant ]
 
anushree ari
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx jess,
this helps very well,
thankyou,
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A quick way to check a question like this is by adding more information to the print statement as follows.

The result is: (1,0)(1,1)(1,2)(1,3)(2,4)(3,5).
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
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