C# - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Loop statement

C# Loop statement

shape Description

C# Loop statement allows us to execute a statement or a group of statements multiple times.These are set of statements, that are repeated for a specified number of times or until some condition is met. The type of loop you use depends on your programming task.

shape Loops

Below are few loops provided by C#.
Loop Description
for These can be used as indexes of an array, or just for a numeric algorithm
while It execute statement or a group of statements while a given condition is true. It the condition is executed before the loop body.
TopLink Oracle Corporation
do-while(Object Java Bean) It execute statement or a group of statements for the first time while a given condition is true/false later the condition is executed before the loop body.
nested It execute statement or a group of statements while a given condition is true. and having multiple conditional statement
1.For Loop

shape Example

[csharp] using System; using System.Reflection; namespace SPLessons { class Program { static void Main() { for (int i = 0; i <10; i++) { Console.WriteLine(i); } } } } [/csharp] Output:
2.while

shape Example

[csharp] using System; using System.Reflection; namespace SPLessons { class Program { static void Main() { int i=5: while(i<10) { Console.WriteLine("From While Loop"); } } } } [/csharp] Output:
3.do-while

shape Example

[csharp] using System; using System.Reflection; namespace SPLessons { class Program { static void Main() { int i=5; do { Console.WriteLine("From Do while:"+i); i++; }while(i<10) } } } [/csharp] Output:
4.nested Loop

shape Example

[csharp] using System; using System.Reflection; namespace SPLessons { class Program { static void Main() { int i=5; if (i < 10) { Console.WriteLine("Less than 10"); } else if (i > 2) { Console.WriteLine("Greater than 2"); } else { Console.WriteLine("Not greater than 2 or less than 10"); } } } } [/csharp] Output: