C# Compared—Do Loop

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

C# Compared to Other Languages

© 2005 C. Gunnerson, et al.

Do Loop

C# has two looping constructs to replace the Do Loop construct. The while statement is used to loop while a condition is true, and do while works the same way, except that one trip through the loop is ensured even if the condition is false. The following VB code

I = 1
fact = 1
Do While I <= n
   fact = fact * I
   I = I + 1
Loop

can be rewritten as:

int I = 1;
int fact = 1;
while (I <= n)
{
   fact = fact * I;
   I++;
}

A loop can be exited using the break statement, or continued on the next iteration using the continue statement.



Previous_Page_.gif Next_Page_.gif


Personal tools