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

While nested in do-while loop

 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Cathy, thanks. I guess i've had too much for the day since i'm missing such simple questions
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic