Loading ...

C# while and do while loop

 /5
0 (0votes)

In this blog, I will explain the "while" and "do while" loop and the difference between them.

The "While" loop

The "while" loop is used in a scenario when one is not sure in advance about how many times the loop needs to be executed. The "while" loop executes a statement or block of statements as long as the condition is true. It is also called entry controlled loop.

 Syntax of while loop is:

  while <condition> statement;

  where while: is a reserved word in c#
  <condition>: is a boolean expression
  statement: can be simple or compound statement
  
 The sequence of operations is as follows:
  1. Test the condition
  2. If the condition is true then execute the statement and repeat step 1.
  3. If the condition is false, leave the loop and go on with rest of the program.


 

The loop is repeated as long as the condition remains true. As soon as the condition becomes false, the control leaves the loop and goes to the next immediate statement after the while loop.

  example of "while" loop:

  public static void Main()
  {
     int i=1;

      while(i<25)
      {
       Console.writeLine("The value is =" +i);
        i++;
      }
   }
 
  Output will be:
 
  The value is = 1
  The value is = 2
  ....
  ....
  The value is= 24


Note:
  1. To get out of while loop we can use a break, return, goto or throw satement.
  2. To pass control to the next iteration without exiting from the loop we use the "continue" statement. "Continue" statement skips the execution of remaining code in the body and starts execution from the beginning of the loop.

 

Nested "while" loop

  The "while" loop can be used inside another "while" loop.This is called nesting of loops. Below is the example of how to use it.

  public static void Main()
   {
      int i=1;

      while(i<25)
     {
        int j=1;
        System.Console.writeLine("The value is =" +i);
        while(j<10)
        {
          System.Console.writeLine("The value is =" +j);
        }
       
        j++;

 

       i++;
     }
   }

 

The  "do  while" loop

It is just like the while loop with the only difference is that here the block of code is executed first and then the condition is checked. If the condition is still true the block will be executed again. It is also called exit controlled loop.

Syntax of  "do while" loop is

  do
   {
     statement
   }
   while<condition>;

   where do : is a reserved word in c#
   statement: can be simple or compound statement
    while   : is a reserved word in c#
   <condition>: is a boolean expression


  The sequence of operations is as follows:
  1.Execute the statement.
  2.Test the condition.
  3.If the condition is true then repeat step 1 to 2.
  4.If the condition is false, leave the loop and go on with the rest of the program.




Difference between while and do while loop

1.In  case of  "while" loop  condition is checked at the starting of the loop and if it is true only then  the code inside the loop will be executed. This process is repeated as long as the condition remains true. In case of "do while" loop the condition is checked at the end of the loop. So even if the condition is false, the code inside the loop will be executed at least once.

 

Comments (1)

   
Anonymous
Need more example and explaination.
8/20/2009
 · 
by
  • :*
  • :*
  • :
 *

Top Posts