Ask your own question, for FREE!
Computer Science 21 Online
OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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;

OpenStudy (e.mccormick):

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.)

OpenStudy (anonymous):

thank you for the help.

OpenStudy (e.mccormick):

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.

OpenStudy (anonymous):

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?...

OpenStudy (e.mccormick):

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).

OpenStudy (anonymous):

great. thank you !

OpenStudy (e.mccormick):

have fun

OpenStudy (anonymous):

Thank you again. The code worked great. ! You guys are such a big help. thank you

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
euphoriiic: I drew more people !! ud83eudd17.
59 minutes ago 40 Replies 3 Medals
euphoriiic: I drew markk ud83eudd17.
2 hours ago 34 Replies 2 Medals
LOLIAteYourMom: u2212243 = u22129(10 + x)
5 hours ago 0 Replies 0 Medals
Bubblezz: Art for @jeromeccv
5 hours ago 13 Replies 3 Medals
Bubblezz: Art for @jeromecv
9 hours ago 0 Replies 0 Medals
MakaylaChuck23: no
4 hours ago 9 Replies 0 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!