Start C# here

Study C#.. Go ahead ! !

Wednesday, June 4, 2008

Loops used in C# .net

Loops are used to run a set of codes repeatedly.

Loops we use in C# are:
  • while Loop,
  • do...while Loop
  • for Loop and
  • foreach Loop

C# while Loop

While loop and do..while loops are used in conditions like the Number of iterations are Unkown ie, the number of times the loop should work is unknown. While loop works only if the Condition gets true.

Let's check the structure of a while loop...

----------------------------------

while(condition==true)
{
//Statements
}

----------------------------------

While loop only works if the Condition is true. Here in this case if the condition doesnt gets false it will become an infinite loop.


C# do ... while loop
-----------------------------------
while loop have a disadvantage that it dosnt gets executed even for one time if the condition is false. To overcome this problem we have do while loop.

do...while loop is also like a while loop but it gets into the loop for the first time even if the condition is false. But from the second time if the loop wants to work the while condition checks for the condition to become true.

The structure of a do...while loop is...

----------------------------------
do
{
//Statements

}while(Condition==true);
----------------------------------

Here from the second time it works only if the Condition or the testexpression becomes true..... In this loop also the number of iterations is not Known.


C# for loop....

For loop is used when the number of iterations is fixed. If we know how many times the loop needs to work we use the For Loop.

The structure of a for loop is...

----------------------------------
for(initialisation;testexpression;increment/decrementoperator)
{
//statements
}
----------------------------------

Here in this case the initialisation is the place we declare or define some values to the variables which should be used inside the loop...
Testexpression checks the condition of the loop ie it defines the number of iteration of the Loop...
increment/decrement This is used to make the condition of the loop to become false so the loop gets stopped at certain interval.

I'll show you an example to get the first ten numbers...

----------------------------------
for(int i=1;i<=10;i++)
{
Console.WriteLine(i);
}
----------------------------------
The above loop will print the first ten numbers. Here itself if we loop at it we can see the Initialisation as int i=1. This is I am declaring a variable with the name i and setting its value as 1. In the second expression I have given a Condition asking whether the variable i have the value less than or equal to 10 and in the third case when each time the loop works the value of variable i gets added by 1. So when it becomes 11 the loop gets terminated.

Working of a for loop...
----------------------------------
While we use a for loop... the initialisation expression is only called for the first time. When the loop starts to run from the second time it doesn't enter to the Initialisation expression instead it increments the value of the variable and checks whether the test expression is true or false.

Step 1: Initialisation
Step 2: Condition
Step 3: Increment or Decrement

For the first time it calls STEP 1
then goes to the Step 2 and enters into the loop body

From the second time when it comes to the loop it goes to Step 3 and then to Step 2..... and if it returns true it enters the loop body...


C# foreach loop...

foreach loop is a new loop introduced in C#. This loop is same as for loop but this loop will be commonly used for working with arrays collections etc.. If we are using a for loop for displaying the values of an array we need to know the size of the array. But in the case of a foreach loop the size of the array need not be known. foreach loop automatically takes the size of the array and calculates it....

Let's see an example for that...

----------------------------------
foreach(DataType var in DataTypeCollection)
{
//Statements
}
----------------------------------
In this type of loop we will be creating a variable which will be the same as the collection datatype to be worked with. If you are going to print all elements of an array collection then the variable should be on the DataType that can store the value of the Array. Like this we can run this for loop even for Controls in .net like ListBox, ComboBox etc.....

Just check how it works.....
----------------------------------
char[] arr=new char[10];
//I need to print all the elements in this array for doing that we can use a foreach loop

for(char c in arr)
{
Console.WriteLine(c);
}
----------------------------------

Here in the above case it will create a Character variable "c" and when the loop starts to work each time it will assign value at the appropriate index to the variable and print it....

No comments: