jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested while loop?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Nested while loop??" Watch "Nested while loop??" New topic

Nested while loop??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031

Hi Guys,

Consider the code below from Dan Chisholm,

How come it prints i1k1i2j1i3j2??

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
I agree. Here's the link: http://aspose.com/file-tools
subject: Nested while loop??
It's not a secret anymore!