Decision making and looping- while and do-while loops in C#

A computer is well suited to perform repetitive operations. Every language must have features that instruct the computer to perform such repetitive tasks. The process of repeatedly executing a block of code is known as Looping. If the looping continues on forever, it is called as infinite looping.In this blog we will be discussing in detail about while and do-while.

In looping, a sequence of statements are executed until some conditions for the termination of the loop are satisfied. A program loop therefore consists of two segments,one known as the body of the loop and the other known as the control statement..The control statement tests certain conditions and then directs the repeated execution of statements contained in the body of the loop.

A loop process generally include the following steps:

  1. setting and initialization of counter
  2. execution of statements in the loop
  3. test for specified condition for execution of loop
  4. incrementing the counter

The c# provides the following loops

  1. the while loop
  2. the do statement
  3. the for statement
  4. the foreach statement

In this blog we will discuss the first two loops (i.e ) the while and do loop in detail


The while statement in C#

The simplest of all the loops that are available in c# is the while loop. The basic format of the while is as follows

initialization
while( test condition )
{
 body of the loop
}

while is an entry controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body the test condition is once again tested and if the condition is true the loop is executed again. This process of repeated execution of the body of the loop stops till the test condition becomes false and the control is transferred out of the loop. On exit, the program executes the statement that is present right after the loop.

The body of the loop may have more than one statement.The braces are required only if the body if the statement contains more than one statement.

consider the following code segment

sum = 0;
n = 1;
while( n<=10)
{
sum = sum + n*n;
n = n+1;
}
System.Console.WriteLine ( " sum is " + sum);

The body of the loop is executed 10 times for n = 1,2 …..10 each time adding the square value of n and the value of  n is incremented each time inside the loop.The test condition may also be written as n<11 , the result would be the same.

Program to print all the odd  numbers between 1 to 10

using System;
public class Solution
{
  public static void Main()
   {
        int n = 1;
        while( n<=10)
         {
           if ( n % 2 == 0)
             {
                n++;
             }
           else
             {
                Console.WriteLine (" " + n);
                n++;
             }
        }
    }
}



output

1 3 5 7 9

The do statement in C#

In the while condition which we have discussed above checks the test condition then executes the body of the loop. Thus the body of the loop is executed only if the condition is true else it is not executed.On some occasions it might be necessary to execute the body of the loop before testing the condition such cases can be handled with the help of do statement. The do statement takes the following form

initialization 
do
{
  Body of the loop
}
while ( test condition);

on reaching the do statement ,the program proceeds to evaluate the body of the loop. At the end the loop the test condition is evaluated. If the condition is true then the loop continues or else if the condition is false the control exits out of the loop.But in do while loop even if the condition is true or false  the loop is executed once if it is true the   loop continues or else the control comes out of the loop

consider the following example

i = 1;
sum = 0;
do
{
sum = sum + 1;
i i+2;
}
while ( sum < 40 // i < 10);



code:

using System;
class Solution
{
    public static void Main()
     {
        int a = 10;
        do
          {
             Console.WriteLine( " value of a" + a);
             a = a + 1;
           }
         while ( a<15);&nbsp;
      }
}

output

value of a 10
value of a 11
value of a 12
value of a 13
value of a 14
value of a 15

This is all about the while and do-while loops in C#. Hope you like this tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *