### Transferring for loop to while loop in a program

I managed to complete the program assignment but out of curiosity, I was wondering if I could transfer this double nested for loop into a nested while loop, I tried my best in doing it but I it is not displaying the similar result as before.

The first set of code is the assignment with the nested for loop, which is right. The second set of code, I am trying to do the same assignment but with a nested while loop, which is not producing the same results as the one with the for loop. Thank you very much.

Here is the assignment:
Write a program that uses nested loops to collect data and calculate the average rainfall
over a period of years. The program should first ask for the number of years. The outer
loop will iterate once for each year. The inner loop will iterate twelve times, once for
each month. Each iteration of the inner loop will ask the user for the inches of rainfall
for that month.

The one with the nested for loop which works.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include using namespace std; int main() { int years; int inches; int average_Rainfall; int enter_Month; int accum1 = 0; int accum2 = 0; int accum3 = 0; cout << "We are going to calculate the average Rainfall over a period of years, please enter the year(s): "; cin >> years; while (years < 1) { cout << "Please enter the years again, can't be lower than 1! "; cin >> years; / } for (int i = 1; i <= years; i++) { cout << "For years: " << i << endl; for (int month = 1; month <= 12; month++) { cout << "Enter the rainfall for month: " << month << endl; cin >> enter_Month; accum2 += enter_Month; } } cout << "The rainfall for these months is: " << accum2 << endl; system ("pause"); }``````

The second set of code is doing this same assignment, but with a nested while loop. This one produces a different output on the compiler for some reason.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include using namespace std; int main() { int years; int inches; int average_Rainfall; int enter_Month; int accum1 = 0; int accum2 = 0; int accum3 = 0; int i = 1; int a = 12; int month = 1; cout << "We are going to calculate the average Rainfall over a period of years, please enter the year(s): "; cin >> years; while (years < 1) { cout << "Please enter the years again, can't be lower than 1! "; cin >> years; } while (years >= i) { cout << "For years: " << i << endl; i++; while (a >= month) { cout << "Enter the rainfall for month: " << month << endl; cin >> enter_Month; month++; accum2 += enter_Month; } } cout << "Total Rainfall: " << accum2 << endl; system ("pause"); }``````
After counting through all 12 months you need to set the months back to 1 for the next year. In the for loop this is taken care of because every time to you enter the for loop month is reset to 1 but in a while loop that doesn't happen, so you have to explicitly write it in. You're going to reset month immediately after you exit its loop. Your code will look like:

 ``1234567891011121314`` `````` while (years >= i) { cout << "For years: " << i << endl; i++; while (a >= month) { cout << "Enter the rainfall for month: " << month << endl; cin >> enter_Month; month++; accum2 += enter_Month; } month = 1;//set month back to 1 for the the next year }``````

One other suggestion, don't use `system ("pause");` and you should be returning 0 at the end of your main function.
That's really weird, and kind of confusing really. I thought the process would be this; I made the outer loop (while loop) runs 3 times, assuming I entered "3" for years, and because the outer loop runs "3" times, doesn't that mean the inner loop (while loop) has to run a set of "3" times?

Why do we have to re-define month even though I already declared it as "1" already? Is it because this only applies to while loops?

Thank you very much.
 Why do we have to re-define month even though I already declared it as "1" already? Is it because this only applies to while loops?

You aren't redefining month in the while loop. You are setting it's value to 1 after changing it to something else. In the for loop, the month variable was redefined every time the loop ran.

You can mechanically replace a for loop with a while loop by using this pattern:

 ``123`` ``````for (init; cond; inc) { // do something }``````
becomes:
 ``123456`` ``````init; while (cond) { // do something inc }``````

so, given this code:
 ``123456789101112`` `````` for (int i = 1; i <= years; i++) { cout << "For years: " << i << endl; for (int month = 1; month <= 12; month++) { cout << "Enter the rainfall for month: " << month << endl; cin >> enter_Month; accum2 += enter_Month; } }``````

If we replace the outer for loop according to that pattern we get:
 ``123456789101112131415`` `````` int i=1; while (i <= years) { cout << "For years: " << i << endl; for (int month = 1; month <= 12; month++) { cout << "Enter the rainfall for month: " << month << endl; cin >> enter_Month; accum2 += enter_Month; } i++; }``````

And then, if you replace the inner for loop according to the pattern:
 ``123456789101112131415161718`` `````` int i=1; while (i <= years) { cout << "For years: " << i << endl; int month=1; while (month <= 12) { cout << "Enter the rainfall for month: " << month << endl; cin >> enter_Month; accum2 += enter_Month; month++; } i++; }``````

You can see that month is actually redefined for every iteration of the outer loop just as it is in the for loop.
 Why do we have to re-define month even though I already declared it as "1" already?

Because at the start of the second time through the inner loop, month won't be 1 any more.
We have to redefine month because we modified it. When you're going through year one, month by month you keep moving the value of month up by 1 until month = 13 which tells you that you're done with that year, so far so good. But now you want to collect the data for year 2, but the value of month is still 13 so it hits your months while loop and it tests is a >= month where a is 12 and months is 13, so it doesn't run and moves off the next year which will have the same problem. Do you see what I'm saying? A good idea is to get a piece of paper and walk through your program and keep track of what each variable's value is. This will let you visually see when something doesn't make sense.
Awesome, now I got it, thanks for the help everyone!
Topic archived. No new replies allowed.