Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested while loop??

 
Joe Harry
Ranch Hand
Posts: 10087
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Consider the code below from Dan Chisholm,



How come it prints i1k1i2j1i3j2??
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

the first while is not the while of the do loop. The while of a do loop is always followed by a semikolon.

The first while plus the following output line really is the statement, that is executed in the do loop.

The second while, followed by a semikolon marks the end of this do while loop. The second while is not an independent while loop with only a ; to execute.


With brackets and better indentation the code looks like


You should now see, why the output is i1k1i2j1i3j2.



Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic