DELETE me
DELETE me

Reputation:

How this looping works in c#

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++; 
}

Upvotes: 7

Views: 312

Answers (8)

Johnny
Johnny

Reputation: 1575

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
}

Upvotes: 0

Ed Gonzalez
Ed Gonzalez

Reputation: 1882

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).

Upvotes: 2

codingbear
codingbear

Reputation: 15063

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.

Upvotes: 0

Jim Burger
Jim Burger

Reputation: 4547

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.

Upvotes: 1

Guffa
Guffa

Reputation: 700372

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.

Upvotes: 1

Mitch Dempsey
Mitch Dempsey

Reputation: 39899

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++; 
}

Upvotes: 13

Felipe Pessoto
Felipe Pessoto

Reputation: 6969

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

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630429

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.

Upvotes: 1

Related Questions