C# Loop Types



C# loop

C# loop used to execute same block of code again and again specified number of time.
Loops execute block of code while the loop condition is true.

C# supports four types of loop.

  • for loop
  • while loop
  • do-while loop
  • foreach loop

C# for loop

for loop – In for loop execute block of code up to the condition is true.
In for loop condition is predefined.

for loop syntax

for ( initialization; condition; increment/decrements)
{
// block of code to be executed
}


C# while loop

C# while loop – In while loop the code of block execute while the specified condition is true.

While loop syntax

while(condition)
{
//block of code to be executed
}


C# do-while loop

In do-while loop always execute block of code at least once, then check condition and repeat the loop as long as the condition is true.

do-while loop syntax

do
{
// block of code to be executed
}
while (condition)


C# foreach loop

In foreach loop used in on array.

C# foreach syntax

foreach (array as value)
{
//bock of code to be executed
}

Subscribe us

If you liked this c#.net post, then please subscribe to our YouTube Channel for more c#.net video tutorials.

We hope that this asp.net c# tutorial helped you to understand about C# Loop Types.


Next asp.net tutorial we will learn about C# For Loop.


Leave a Reply

Your email address will not be published. Required fields are marked *