• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need some help understanding the last portion of this simple loop.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is what I understand:
the for loop starts, with int i equal to 5 and j equal to 3. Since i > j, it runs the statement (Do While loop). The Do While loop prints the values to the screen. Then since j is equal to 3, it will do the Do While loop again, first decrementing j by 1. So j is 2 and i is 5.. those get printed. Then since j is not >= to 3, the loop ends and backs out to the For loop.

Here is where I am confused. I know that j gets decremented to 1 before it backs to the For loop. I understand everything so far. However, I know that the Do While loop runs again, printing 1 for j and 2 for i. So I gather that since i was greater than j, 3 was subtracted from i... then the Do While ran, printing the new values... and stopped because j isn't >=3. So j becomes zero (though it won't ever print that) but I don't understand how the loop finally ends.

Any hints? I tried looking at code examples in several books but none have that kind of thing.. only ones that talk about when i is less than a value to stop when that boolean is false. However in this code, i so far has always been greater than j.

What makes the loop end?

Thanks in advance,

SYD
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Syd

Always use %n after printf, rather than \n.

You start your for loop with i = 5, j = 3, and as you correctly say, the do loop runs twice, leaving j = 1. Then i is reduced by 3, to 2.
Then your for loop runs a second time, and you start your do loop. A do loop always runs once, then it tests for its continuation condition. The do loop therefore runs a 3rd time . . .
Then, j = 0, and i is reduced again by 3 to -1, so i > j returns false and the for loop terminates.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you say simple loop?

You now know why you don't use that sort of code in real life!
 
SYD O'Reily
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense! Thank you so much. =)

 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic