5.3.The for Loop

A for loop works like a while loop, except that the syntax of the for loop includes initialization and condition modification. for loops are appropriate when you know exactly how many times you want to perform the statements within the loop. The contents within the for loop parentheses hold three sections separated by semicolons (; ; ) { }.

for(Initialization ; Expression ; Iteration)

{

Statements…

}

The For Loop: ForLoop.cs

using System;
class ForLoop
{
   publicstaticvoid Main()
    {
        for (int i=0; i < 20; i++)
        {
            if (i == 10)
                break;

            if (i % 2 == 0)
                continue;

            Console.Write("{0} ", i);
        }
        Console.WriteLine();
    }
}

Yes No Suggest edit
Suggest Edit