views:

252

answers:

8

Recently, in a book i read this code. Can you define the means of this code and how this code works.

int i = 0; 
for (; i != 10; ) 
{ 
    Console.WriteLine(i); 
    i++; 
}
+13  A: 

It loops.

Since you already set i=0 above, they have omitted that section of the for loop. Also, since you are incrementing the variable at the end, they have omitted that as well.

They basically just turned a for loop into a while loop.

It would probably be more elegant as:

int i = 0; 
while( i != 10 ) 
{ 
    Console.WriteLine(i); 
    i++; 
}
webdestroya
as well as initializing i outside of the loop to give it a bigger scope, it also allows the incrementor to be done upon a condition within the loop, e.g. i+=2 or i-- or something, without having to compensate for whatever was specified in the third part of the for loop. But yes, same as a while I think
Jacob
@Jacob, +1 for the scope catch, I didn't even think about that.
webdestroya
+2  A: 

If it's easier to see in normal form, it's almost the equivalent of this:

for (int i = 0; i != 10; i++) 
{ 
    Console.WriteLine(i); 
}

With the exception that it leaves i available for user after the loop completes, it's not scoped to just the for loop.

Nick Craver
+2  A: 

It's the same as this loop:

for (int i = 0; i != 10; i++) {
  Console.WriteLine(i); 
}

Except, the variable i is declared outside the loop, so it's scope is bigger.

In a for loop the first parameter is the initialisation, the second is the condition and the third is the increment (which actually can be just about anything). The book shows how the initalisation and increment are moved to the place in the code where they are actually executed. The loop can also be shown as the equivalent while loop:

int i = 0;
while (i != 10) {
  Console.WriteLine(i);
  i++;
}

Here the variable i is also declared outside the loop, so the scope is bigger.

Guffa
+2  A: 

You are using "for", like a "while"

Fujiy
+2  A: 

This code could be rewritten, (in the context of your code snippet - it is not equivalent as stated.) as:

for (int i = 0; i != 10; i++)
    Console.WriteLine(i);

Basically, the initializing expression and the increment expression have been taken out of the for loop expression, which are purely optional.

Jim Burger
I would note that what you posted is not *equivalent*, there is a scoping difference.
Nick Craver
Thanks @Nick, edited as per your suggestion.
Jim Burger
+1  A: 

It is same as:

for (int i = 0; i != 10; i++) {
    Console.WriteLine(i);
}

Please don't write code like that. It's just ugly and defeats the purpose of for-loops.

codingbear
+3  A: 

The for statement is defined in the C# spec as

for (for-initializer; for-condition; for-iterator) embedded-statement

All three of for-initializer, for-condition, and for-iterator are optional. The code works because those pieces aren't required.

For the curious: if for-condition is omitted, the loop behaves as if there was a for-condition that yielded true. So, it would act as infinite loop, requiring a jump statement to leave (break, goto, throw, or return).

Ed Gonzalez
A: 

As int i was declared on top so it was not in the for loop. this is quite like

for(int i = 0; i!=10; i++)
{
   /// do your code
}
Johnny