Looping Statements:
1) for loop
2) while loop
3) do while loop
4) for each loo
Syntax for for loop:
using
System;
class
ForLoopTest
{ static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i); }
} }
Output:
1
2
3
4
5
Syntax for while
loop:
While ()
{
Statements
[increment/decrement]
}
Example:
using System;
class WhileTest
{ static void Main()
{
int n = 1
while (n < 6)
{ Console.WriteLine("Current
value of n is {0}", n);
n++;
} } }
Syntax fro do:
do
{ Statements
[increment/decrement]
} while condition
Example:
using System;
public class TestDoWhile
{ public static void Main ()
{ int x = 0;
do
{ Console.WriteLine(x);
x++;
} while (x
< 5); } }
Syntax for for each
loop:
The foreach statement repeats a group of embedded
statements for each element in an a rray
or an object collection.
Foreach(datatype variable in array/list collection name)
{ Statement
}
Example
class ForEachTest
{
static void Main(string[]
args)
{ int[] fibarray = new int[] { 0, 1, 2, 3,
5, 8, 13 };
foreach (int i
in fibarray)
{
System.Console.WriteLine(i);
}
}
}
No comments:
Post a Comment