+ 1

What happens when the initialization and condition parameters in the FOR loop are omitted?

I've seen this case on quizzes and didn't know what the code stood for.

7th Feb 2017, 7:05 PM
Martín Lehoczky
Martín Lehoczky - avatar
2 ответов
+ 1
So what happens when there is no initialization and condition parameters in for loop. It looks like this: int i = 0; for( ; ;i++){ } In the case above, it is the infinite loop. If "i" wasn't initialized above, you would get compile time error.
7th Feb 2017, 7:14 PM
Ladislav Milunović
Ladislav Milunović - avatar
+ 1
The for loop is something like for (int i=0; i <10; i++){ //... } and that tells the compiler int i =0 : here is the increment variable for the loop and it's initial value is 0. If you don't declare an incrementor, the compiler will accept that you don't need one. i <10 : Run the for loop as long this boolean expression is true. If you omit that expression, the compiler will interprete that as true. Every time. The compiler hopes you know what your doing 😊 i++ : The increment (decrement is possible as well of course), that's just an example. Next time the loop runs the boolean expression mentioned above will be tested with the new incremented (or decremented) value. If you don't declare an incrementor, you cannot increment. So you are allowed to omit that, too. So when you have for ( ; ; ) you have an infinite loop.
7th Feb 2017, 7:31 PM
Tashi N
Tashi N - avatar