my dog learned polymorphism*
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "While nested in do-while loop" Watch "While nested in do-while loop" New topic

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)
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
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:
subject: While nested in do-while loop
Similar Threads
Break and continue inside the case statement
one more doubt regarding operators
how nested loop works?
Dan's Exam on Flow control
Chisolm mock exam query