C# Loops - c# - c# tutorial - c# net



learn csharp tutorial - c# Control Structure - c# example programs

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
 c sharp 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:

while (condition is true) 
{
    code will be executed;
}

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#

for loop in C#

Syntax:

for (initialization counter; test counter; increment counter) 
{
    code to be executed;
}

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

do while loop

Syntax:

do 
{
    code to be executed;
} 
while (condition is true);

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

c sharp for each loop

Syntax:

 foreach (string name in arr)
 {
 }


Related Searches to C# Loop