File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Storm Applied this week in the Other Open Source APIs forum!
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: 9811

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
It is sorta covered in the JavaRanch Style Guide.
subject: Nested while loop??