Ask your own question, for FREE!
Computer Science 9 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
laylasnii13: Can i dm anybody to vent having a rough time ??
9 hours ago 1 Reply 0 Medals
kaelynw: starting to draw a hand
1 minute ago 16 Replies 2 Medals
Twaylor: Rate it :D (Took 2 days)
9 hours ago 7 Replies 0 Medals
XShawtyX: Art, Short Writing Assignment: Imagining Landscapes
9 hours ago 4 Replies 1 Medal
XShawtyX: Chemistry, Help ud83dude4fud83cudffe
1 day ago 13 Replies 1 Medal
kaelynw: tried a lil smt, the arm is off but i like the other stuff
1 day ago 27 Replies 3 Medals
kaelynw: art igg
1 day ago 14 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!