• Have Any Question?
  • contact@eitworld.com

csharp loops





Iteration statements

 

  • Iteration statements enable the user to perform the same set of statements repeatedly until a specific condition is met.
  • Iteration statements also known as looping statements.
  • In C# there are following iteration or looping statements are present:
    • while loop
    • for loop
    • do-while loop
    • foreach loop

Now we are going to explain each of these looping statements in detail:

 

while loop

 

    • The while loop is a simple and most commonly used loop.
    • while loop repeatedly executes a block of statements until specified condition get false.
    • while loop checks the condition before entering the first iteration.
    • The basic syntax of while loop is as follows:
Syntax

 

Initialization;
while(condition)
{
Body of while loop
}

Body of while loop repeatedly executes as long as condition is true. When the condition becomes false the program control shifted to the statement present just after the while loop.

 

Here is an Example of the while loop which prints the values from 1 to 10:

 

Example

 

using System;
namespace Iteration
{
class whileloop
{
static void Main()
{
int i = 1;
while (i <= 10)
{
Console.WriteLine(i);
Console.WriteLine(“”);
i++;
}
Console.ReadLine();
}
}
}

Output

 

1
2
3
4
5
6
7
8
9
10

for loop

 

    • The function of for loop is similar to the while loop the only difference is that its syntax is different from the while loop.
    • for loop also repeatedly executes a block of statement until specified condition get false.
    • for loop is good for those situations where you exactly know how many time you have to execute the statements within the body of loop.
    • It also checks the condition before entering the first iteration.
    • The basic syntax of for loop is given below:
Syntax

 

for (initialization;
condition;
increment/decrement)
{
Body of for loop
}

Let us take a look to the process of for loop:

 

  • In the above syntax of for loop you can see the first term as ‘initialization’ which allows the user to initialize the value of loop counter and initialization is done first in the whole process of for loop.
  • Next term is ‘condition’. If the condition is true loop body get executed and if it is false the control of program shifted to the statement just after the ‘for’ loop.
  • Next term is ‘increment/decrement’ after the execution of the body the program control passes to this statement and loop counter get updated either increment or decrement.
  • Now steps 2 and 3 will repeated until the condition get false.

Here is an Example of for loop which will print the values from 1 to 10:

 

Example

 

using System;
namespace Iteration
{
class forloop
{
static void Main()
{
for(int i=0;
i<=10;
i++)
{
Console.WriteLine(i);
Console.WriteLine(“”);
}
Console.ReadLine();
}
}
}

The output of the above program is given below:

 

Output

 

1
2
3
4
5
6
7
8
9
10

do-while loop

 

    • do-while loop is similar to the while loop but the difference is that it checks the condition after the execution of the loop-body.
    • do-while loop certainly execute once whether condition is true or false.
    • The basic structure of the do-while loop is given below:
Syntax

 

Initialization
do
{
Body of the do-while loop
}
while(condition);

  • Here you can see the condition is present at the end so the loop body certainly executes once.
  • When condition becomes false the program flow jump on the statement just after the do-while loop.

Here is an Example of do-while loop which prints the value from 1to 10.

 

Example

 

using System;
namespace Iteration {
class dowhileloop {
static void Main() {
int i=1;
do {
Console.WriteLine(i);
Console.WriteLine(“”);
i++;
}
while(i<=10);
Console.ReadLine();
}
}
The output of the above program will be:

Output
1
2
3
4
5
6
7
8
9
10

foreach loop

    • foreach each loop is used to iterate the elements present in arrays.
    • An array is a collection of the values of same data type. We will discuss the array in the futher section.
    • foreach loop is preferred there where you want to go through the all elements of an array.
    • The basic syntax of foreach loop is given below:
Syntax
foreach(type variable in expression)
{
Body of foreach loop
}

  • In the above syntax type variable shows the iterative elements and expression is the name of array.

Here is an Example of the foreach loop which prints the all elements present in an array:

Example
using System;
namespace Iteration
{
class foreachloop
{
static void Main()
{
int[] arr = {
10, 20, 30, 40, 50 }
;
foreach(int i in arr)
{
Console.WriteLine(” ” + i);
}
Console.ReadLine();
}
}
}

The output of the above program is given below:

Output
10
20
30
40
50

Nested Loop

    • When any loop is used inside the other loop that is known as nested loop.
    • You can use for loop inside while loop, do-while loop inside for loop, while loop inside while loop or so on.
    • The basic syntax of nested loop is given below:
Syntax
for(initialization;
condition;
increment/decrement)
{
while(condition)
{
……………………
……………………
}
}

Nested loop can have various structures. Here is an Example of nested loop in which we are using the for loop inside a for loop.

Example
using System;
namespace Iteration
{
class nestedloop
{
static void Main()
{
for (int i = 1;
i <= 3;
i++)
{
Console.WriteLine(“You are in Outer loop”);
for (int j = 1;
j <= 3;
j++)
{
Console.WriteLine(“You are in inner loop”);
}
}
Console.ReadLine();
}
}
}

In the above program first outer loop checks the condition for i=1 this is true so the program control get inside the outer for loop and print the message “You are in Outer loop”. Now it will get inner for loop it executes the inner loop 3 times and print the message “You are in inner loop: three times. Now again the control goes to the outer for loop and again print the message “You are in outer loop” and also execute the inner for loop 3 times. This process will continue until the condition of outer for loop get false.

The output of the above program is given below:

Output
You are in Outer loop
You are in inner loop
You are in inner loop
You are in inner loop
You are in Outer loop
You are in inner loop
You are in inner loop
You are in inner loop
You are in Outer loop
You are in inner loop
You are in inner loop
You are in inner loop



Real Time Web Analytics