Wednesday, 15 April 2015

Infinite for loop in C++ -



Infinite for loop in C++ -

while reading in code, found loop used 2 arguments (the 1 in middle absent). when programme executed, loop infinite. here minimal working environment. tell wrong in code?

// illustration programme #include <iostream> using namespace std; int main() { (int = 0; ; = (i+1)%2) { cout << << endl; } }

there nil wrong code. allows infinite counting (to int's limit is) until internal status stops it. none of 3 parts of loop necessary, of below valid.

for(;;); // yes, never stop for(int x = 0;;){ } for(int x = 0; x < 10; ++x); for(; somecondition != true;){}

and on, parts optional, body.

a reason might used if 1 thing done data, need stop, if thing, want it. however, @ same time, need know how many times it's happened. you'll utilize without parameters eventually, now, understand have valid loop, have have top line in code snippet above.

c++ for-loop

No comments:

Post a Comment