C# double & nary for loops

Posted by MarkPearl on Geeks with Blogs See other posts from Geeks with Blogs or by MarkPearl
Published on Sat, 25 Jun 2011 12:29:43 GMT Indexed on 2011/06/25 16:22 UTC
Read the original article Hit count: 184

Filed under:

 

Something that I wasn’t aware was possible in C# was double for loops… they have the following format.

for (int x = 0, y = 0; x < 10; x++, y=x*2)
{
    Console.Write("{0} ", y);
}                  
Console.ReadLine();

This would give you the following output…

0 2 4 6 8 10 12 14 16 18

In fact you can use as many variables as you want, the following would also be valid…

for (int x = 0, y = 0, z = 10; x < 10; x++, y=x*2)
{
    Console.Write("{0} ", z);
}                  
Console.ReadLine();

© Geeks with Blogs or respective owner