Can anyone help please!! Racking my brain and can't figure out..? I need to know how to convert a nested for loop into a nested while loop to create an output that shows numbers 1-12 and their factorial. I know how to do it using two for loops. But I can't figure out how to do it using a for loop & a while loop?
when I write it with two for loops it works fine. (below) int i, j; int num; for (i= 1; i <=12; i++) num=1; for (j=1; j<=i ; j++) num = num * j; cout << i << "Is" << num << endl;
a while loop goes on while a condition is true. ``` int i = 0; while i < 12{ i++; cout << "i is " << i << endl; } ``` Oh, and puting ``` above and below a code block causes the code quoting system to turn on. (That is the one with the ~ on the key.)
thank you for the help.
Another thing people sometimes do is make a flag. bool flag = true; while (flag) { do some work some if thing to see if you need to set flag to false more work another if thing to see if you need to set flag to false } As shown, this is most useful when more than one thing may need to change the flag to false.
well, I was just trying to change the inner for loop into a while loop, and kept failing. but i'll try this. Can I keep the first outer for loop the same though?...
yes. You can do either one or both using these sorts of methods. You can also use a do while loop, which is a little more like a for. do{ bla bla } while (condition); This has the advantage that it always happens one, which is more like a for, but takes a little more typing (not much).
great. thank you !
have fun
Thank you again. The code worked great. ! You guys are such a big help. thank you
Join our real-time social learning platform and learn together with your friends!