Thursday, 1 February 2018

006 C# Loop

C # tutorial 006 Loop 
Loop is a part of a program to do anything continuously and repeatedly until a condition satisfied. Sometimes we do some work, until we reach the goal, sometimes we repeatedly do something to reach any conclusion. For example, to find , how many 3 are there in 15. We can go adding 3 until we reach 15. The first  3 + 3 will give us 6 , the Second time 6 + 3 will give us 9, condition is to reach 15. Then we go adding 3 until you reach 15. This is the concept of loops. Continuously doing the same thing until the condition is reached. 

There are mainly three kind of loop
  •   While loop 
  •   For loop 
  •   Do while loop 
Another kind of loop, that is nested loop. Nested loop can be any loop , do while, while, for.

 1)While loop 

It is a control structure loop where entry is controlled once. In while loop, the condition of exit the loop comes under while statement. Until the condition is false  the loop will execute repeatedly. And under this loop, if there is any program statement , that will execute repeatedly.






inti = 1; 

Example 1
while (true)
{
     //required code
}
 
Example 2

while (i < 4)
{
       Console.WriteLine(i.ToString());
}
//Output:1,2,3

2)For loop

For loop also entry control loop which will execute repeatedly. It's action is predefined number of times of repeatation. The syntax of for loop as below. Fast a variable is defined and initialise, then

the exist condition  is written, then the variable which was initialised, step is written incremental / decremental to satisfy the exit condition. One major difference for for loop other than , while and do while loop,  that variable can be defined in the loop statement directly. Here is the example of follow which show how a variable is defined within the loop.


Syntax

for(initialization; condition; step)

Example 


for (int i = 1; i <5 font="" i="i+1)">
{
      Console.WriteLine(i.ToString());
}
//Output :1,2,3,4
 

3)Do while loop

Do while loop also loop which to repeat work until the condition satisfied. It is an exit control loop. Depending upon the condition, if satisfied, the control send back to the loop again. In this kind of loop , the condition is checked finally and decision is taken whether the loop will continue or exit the loop.

Syntax 


do
{
    //statement
} while (C
ondition is true)
 
    
int i = 1;
do
{
         Console.WriteLine(i.ToString());
} while (i <= 10);


//Output :1,2,3..10

Here is the difference between loops


while

for

do while
Entry control Entry control Exit control
Not predefined reparation Predefined reparation Not predefined reparation


Infinite Loop


An infinite loop is a loop which is executed endlessly, no termination condition satisfy to terminate the loop or the loop was instructed continuous repeat. Infinite loop maybe while loop, do loop, for loop. In this kind of loop, if we do not write termination condition properly, the loop will execute endlessly. Here are the example of infinite loop.

Example 1

while (true)
{
}

Example 2
for (; ; )
{
}

Example 3
do
{
} while (true);



Break and Continue
 
Break is a statement to break a loop which is running. Break statement break the existing loop and return that control to the program. Generally break is written when are in a particular situation the loop need to be stop. Continue is a statement to continue the loop to next Iteration . When the continue statement is executed the control of the loop the to next Iteration . In the below example, continue statement execute, it goes to the Iteration work equal to 26.

for (int i = 1; i < 100; i++)
{
if (i>50)
        break;//
if (i==25)
        continue;
         Console.WriteLine(i.ToString());
}


Nested loop
Nested loop is loop which comes under any loop. Can be a for loop under a while loop, while loop under a do loop. The below is example of nested loop. The nesting of loops level is not constant, there may be multiple level of nesting depending upon the situation, . Net I have no control on restriction over the level of nesting. Break and continue statement can be implemented each level of nesting.

Example 1 
for (int i = 1; i < 100; i++)
{
         for (int l = 1; l < 10; l++)
         {
           //statment
         }
}

Example 2 
int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

for (int i = 0; i < 4; i++)
{
                   for (int l = 1; l < 2; l++)
                    {
                          //statment
                            var val = myArray[i, l];
                           Response.Write(val+"");
                     }
}

//Output:
2
4
6
8


The below example is a sample of nested loops. And multidimensional array is initialized with data 
and nested loop is created to access this data. This a nested loop can be created any level.
 Generally,to access long data set , array nested loop is used. Below is a example of nested loop 
with break statement.

Example


int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
for (int i = 0; i < 4; i++)
  {
          for (int l = 1; l < 2; l++)
           {
               //statment
                var val = myArray[i, l];
                if (val==6)
                 break;//
           }
}


No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...