My rule of thumb is to use for loops if I have control over when the loop will stop---for example if I want it to work X times or if I want it loop while X < Y, etc, AND if the increment I want to make is the same each time through the loop. If those criteria can't be met then I use a while or do/while loop. I usually use a while loop unless I want the while loop to run at least once, no matter what, in which case I use a do/while loop.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
That loop is saying that you are going to start the loop with i initialized to 0, and increment i by 1 for each iteration. You will keep looping as long as i is less than 5.
So, the first time through the loop i will be 0, then 1, then 2, then 3, then 4.
Then, i will be set to 5, and the condition "i <= 5" will fail so the loop will be exited. So, you are doing j = 2*j + 3; five times.
I think you mean when i is set to 6 it will fail, thus 5 iterations.
In relation to the original question, I would use a while loop with the test condition checking against the ever decreasing balance owed. Each iteration equating to a month.
superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
Oops, lol. I did mean 6, but that would make it 6 iterations.
Oops straight back at ya.......lol
superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3