• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do While loop Conundrum  RSS feed

 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across a question for Do While loops in Mala Gupta's study guide. I have added some "sysout" statements to try to understand it better plus one set of curly braces (after the first do before the second while). I saw a similar question posted on another forum and I still don't understand the reason behind the answer given.

I would expect the following code to go as follows:
i = 10
Enter the "Do" portion of the loop
i++ makes i = 11
The condition of i < 15 is true b/c i is only 11
i + 20 makes i = 31
The second while loop condition executes and fails b/c 31 is not < 2
The final print statement executes and prints 31.

Expected output:
11
31
31


HOWEVER....that's not what happens. For some reason, even after the bottom while condition fails, the top portion of the "Do" loop executes again??? Originally I was thrown off by the whole thing having two separate "While" portions for only one "Do" portion. Why does that top portion of the "Do" statement execute after the flow control "While" statement fails? Doesn't that end the loop?

The actual output is:
11
31
32

The only reasonable thing my head can come up with is that the second "While" condition doesn't get to execute if the first one is true b/c the first one then sends it back to the start of the "Do" loop??? Is that what's happening b/c the first while portion does end with a ";" so I wasn't sure if it was even a true "while" statement?



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what happens:

i is set to 10
line 6: i++ sets i to 11, and the expression is true because 11 < 15 => we enter the while loop
line 7: print the value of i, which is 11
line 8: i is set to i + 20 = 11 + 20 = 31
line 9: print the value of i, which is 31
then we are at the end of the inner while loop, we go back to line 6
line 6: i++ sets i to 32, and the expression is false because 32 >= 15 => we skip the while loop
we are at line 11: the expression i < 2 is false because i == 32 => the do-while does not repeat
line 12: print the value of i, which is 32
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Newman wrote:
I would expect the following code to go as follows:
i = 10
Enter the "Do" portion of the loop
i++ makes i = 11
The condition of i < 15 is true b/c i is only 11
i + 20 makes i = 31
--->The second while loop condition executes and fails b/c 31 is not < 2

The first loop has to end before you exit it so once I is 31 and you print that the first time, the loop goes back to the top on line 6. the "while (i++ < 15){" runs again, causing i to be incremented again, making it 32. THEN we exit this loop, and hit the while on line 11.
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! I really appreciate the responses! That answered my question! The first while functions as it's own loop (despite not having a semicolon at the end), making it an inner loop rather than the partnered "while" part of the "Do While" loop. That makes it run twice, and the i variable is incremented twice before it fails.

Thank you again!
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
line 6: i++ sets i to 11, and the expression is true because 11 < 15 ...


Just a minor correction.
The comparison is done against the original value of 'i', not the post increment value, so it's comparing 10 with 15.

You can see this easily by starting i off as 14, and it will still produce 3 results...15, 35 and 36.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody in UK will understand the line missing from that code. Rather than being blank, line 13 (how appropriate!) should read
throw new P45Exception(";-)");

… because nobody would write such code in real life and live to tell the story.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's one of the reasons I strongly suspect I would fail the certification exam...
Some of the problems that come up in those are just mad.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Just a minor correction.

Yes Dave, you're right. Well spotted.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!