Ask your own question, for FREE!
Computer Science 13 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
JOSEPHh: I did one of Patrick as well
6 hours ago 67 Replies 5 Medals
JOSEPHh: Who likes my drawing?
6 hours ago 20 Replies 3 Medals
emmarose: What is your earliest childhood memory?
1 day ago 13 Replies 0 Medals
emmarose: what are y'all's opinion on squirrels
1 day ago 18 Replies 0 Medals
Rosa2011: It's my birthday, what usually y'all doing on your birthdays
1 day ago 28 Replies 4 Medals
Skyler14: we just got a new puppy
1 day ago 20 Replies 1 Medal
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!