A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
C# programming language provides the following types of loop:
for
while
do while
foreach
Syntax of for loop:
(1)
for(initialization; conditions;update_statements)
statements
(2)
for(initialization; conditions;update_statements)
{
block of statement
E.g.
main()
{
int a;
for(a=0;a<10;a++)
Console.WriteLine(" hello {0}",a);
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
E.g.
main()
{
int a;
for(a=0;a<10;a++)
{
Console.WriteLine(" hello ");
Console.WriteLine("%d",a);
}
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
Syntax of while loop:
while(condition)
{
statement's
}
E.g.
main()
{
int a=0;
While(a<10)
{
Console.WriteLine(" hello ");
Console.WriteLine(a);
a++;
}
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
E.g.
main()
{
int a=0;
While(a<10)
{
Console.WriteLine(" hello {0}",a);
a++;
}
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
Syntax of do while loop:
do
{
statement's
}
while(Condition);
E.g.
main()
{
int a=0;
do
{
Console.WriteLine(" hello ");
Console.WriteLine(a);
a++;
}
While(a<10);
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
E.g.
main()
{
int a=0;
do
{
Console.WriteLine(" hello {0}",a);
a++;
}
While(a<10);
}
output:
C# programming language provides the following types of loop:
for
while
do while
foreach
Syntax of for loop:
(1)
for(initialization; conditions;update_statements)
statements
(2)
for(initialization; conditions;update_statements)
{
block of statement
}
E.g.
main()
{
int a;
for(a=0;a<10;a++)
Console.WriteLine(" hello {0}",a);
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
E.g.
main()
{
int a;
for(a=0;a<10;a++)
{
Console.WriteLine(" hello ");
Console.WriteLine("%d",a);
}
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
Syntax of while loop:
while(condition)
{
statement's
}
E.g.
main()
{
int a=0;
While(a<10)
{
Console.WriteLine(" hello ");
Console.WriteLine(a);
a++;
}
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
E.g.
main()
{
int a=0;
While(a<10)
{
Console.WriteLine(" hello {0}",a);
a++;
}
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
Syntax of do while loop:
do
{
statement's
}
while(Condition);
E.g.
main()
{
int a=0;
do
{
Console.WriteLine(" hello ");
Console.WriteLine(a);
a++;
}
While(a<10);
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
E.g.
main()
{
int a=0;
do
{
Console.WriteLine(" hello {0}",a);
a++;
}
While(a<10);
}
output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
foreach:
foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate trough the collection to get the desired information but should not be used to change the contents of the collection to avoid unpredictable side effects.
Syntax:
foreach(type identifier in expression)
statements
eg:
int ev=0,od=0;
int [] arr=new int [] {0,1,2,3,4,5,6,7,8,9};
foreach(int a in arr)
{
if(i%2==0)
en++;
else
od++;
}
Console.WriteLine("odd numbers{0} even numbers{1}",en,od);
No comments:
Post a Comment