Looping Constructs                         
                                                      
C# shares many looping constructs with C, C++, and Java.  For example, you can use the standard while,
do/while, and for loops.


while(boolean expression)
{  // loop while expression is true.
}

do
{ // loop while expression is true. Bottom evaluation means
 // at least one iteration.
}while(boolean expression);

for(int i = 0; i < 100; i++)
{ // Do while i < 100. i is incremented with each iteration.
}


C# also provides the foreach loop construct, which is convenient for iterating over arrays and collections.


static void Main(string[] args)
{    
  // The standard for loop.
  for(int i = 0; i < args.Length; i++)
  {
     Console.WriteLine(args[i]);
  }

  // The foreach loop. No counter variable or array bounds
  // checking required.

  foreach(string arg in args)
  {      
     Console.WriteLine(arg);
  }
}

C# Looping
Table of Contents
C# Tutorial | C#.NET Tutorial | C# Looping Tutorial

Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used
exclusively as an online learning aid.  Any attempts to copy, reproduce, or use for training is
strictly prohibited.
Courseware
Training Resources
Tutorials
Services