GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes While nested in do-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 "While nested in do-while loop" Watch "While nested in do-while loop" New topic
Author

While nested in do-while loop

geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
Why would the output be 012? Wuldn't the do loop be executed 3 times?
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
do
while (i++ < 3)
System.out.print(k++); //prints 012
while (j++ < 3);
Outer while does loop 3 times, but i has already incremented and is greater than 3, so inner while condition is false and nothing is printed.
I hope this helps.
geeta rai
Ranch Hand

Joined: Sep 18, 2003
Posts: 48
Hey Cathy, thanks. I guess i've had too much for the day since i'm missing such simple questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: While nested in do-while loop