• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

loop  RSS feed

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
thx jess,
this helps very well,
thankyou,
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • 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).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!