C# Loops - c# - c# tutorial - c# net
c# Control Structure
What are the types of loop in C# ?
- In C#, looping statements repeat a specified block of a code, until a given condition is true. C# provides following types of loop to handle looping requirements. Such as,
- While loop
- For loop
- Do while loop
- Foreach loop
While loop:
- In C#, the while statement executes a statement or a block of statements until a specified expression evaluates to false.
- While loop
- For loop
- Do while loop
- Foreach loop
Syntax:
For loop:
- For loop statement is executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
for loop in C#
Syntax:
Do while loop:
- The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false.
- The body of the loop must be enclosed in braces, {}, if it consists of a single statement.
do while loop
Syntax:
Foreach loop
- The foreach statement is used to iterate through the collection to get the information that we want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects.
- If we need to add or remove items from the source collection, use a for loop.
- A foreach loop can also be left by the goto, return, or throw statements.
c sharp for each loop