0
How many times this code is running?
#include <stdio.h> int main() { int x; for(x=-1;x<=10;x++) { if(-1<5) continue ; else break ; printf ("DOEACC"); } return 0; }
3 Antworten
+ 10
0 times when it is not inserted in the code playground and linked to Q&A. 🙄
+ 8
You can find it out by actually running the code.
Q&A is for getting help with code, it is not a quiz section.
+ 3
It runs once each time you press the Run button. 🤓🤭
Assessment:
The loop repeats according to: the limits supplied, the logic inside the loop, and whether the optimizer works out a better way to do it.
Analysis:
The loop limits are from -1 through 10, inclusive. That means it is set up to loop 10 - (-1) + 1 = 12 times.
The loop logic has nothing that causes early termination. Just beware that all the code past the continue statement is unreachable because the if conditional is constant and always true.
At compile time, the optimizer will recognize the uselessness of actually running this loop, and replace the loop with a simple assignment of x = 11. But in the next pass, it would recognize that x is never used, so even the x=11 would get removed.
I am guessing the compiler gives a number of warnings about unreachable code and maybe the unused variable x.
In the final analysis, the loop would run zero times. The program would just start up and then exit immediately, having run once and done nothing.