Execute 10 Times | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Execute 10 Times

How do you get this code to execute 10 times? I can only get 3 exes. Include the for loop below in a small program named question2a.cpp and complete the program. The loop should be executed 10 times. Do not change the for loop below. Compile and run your program to see for yourself that it works. You do not have to submit this program and output. for (int i = 1; i <= n; i++) if (i < 5 && i != 2) cout << 'X';

26th Aug 2017, 7:40 PM
gorgamin
gorgamin - avatar
4 Answers
+ 9
if (i < 5 && i != 2) corresponds to values 1, 3, and 4. This is why the print statement is only executed three times.
27th Aug 2017, 2:45 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Output is not 1:1 to iterations. Since you don't have to submit the code, you could change what's inside the loop and -- as it says -- prove to yourself that you have the correct number of iterations.
26th Aug 2017, 9:46 PM
Kirk Schafer
Kirk Schafer - avatar
0
Thanks! The question says I'm not supposed to change the FOR LOOP, but if I change the IF LOOP, it works, right? I didn't notice there's a for and an if. if (i<12 && i !=2)
27th Aug 2017, 7:13 AM
gorgamin
gorgamin - avatar
0
Now convert the for loop into a while loop and add any variable initializations that you think are necessary
22nd Sep 2019, 6:55 PM
Reneilwe Mai-Tee Naso
Reneilwe Mai-Tee Naso - avatar