do...while loop

In this chapter you will learn:

  1. How to use do...while loop
  2. Using break to exit a do-while loop
  3. How to use do while loop to find the square root of a number
  4. Do / while loop with a console read

Using do...while loop

do..while loop checks the loop control bool expresion after executing the loop body. Therefore the loop body gets executed at least once.

The do..while has the following form.

do{
  loop body;
  
}while(bool expression)

We can change the while loop to do while loop.

using System;//j  a  va 2  s. c  o m

class Program
{
    static void Main(string[] args)
    {
        int i = 5;

        do
        {
            Console.WriteLine(i);
            i--;
        } while (i > 0);

    }
}

The output:

The following code displays the digits of an integer in reverse order with do...while loop.

using System; /*from j a  v a  2 s .  com*/
 
class MainClass {   
  public static void Main() { 
    int num; 
    int nextdigit; 
 
    num = 198; 
 
    Console.WriteLine("Number: " + num); 
 
    Console.Write("Number in reverse order: "); 
 
    do { 
      nextdigit = num % 10; 
      Console.Write(nextdigit); 
      num = num / 10; 
    } while(num > 0); 
 
    Console.WriteLine(); 
  }   
}

The code above generates the following result.

Using break to exit a do-while loop

using System; //  j a v  a 2 s .  co m
 
class MainClass {  
  public static void Main() {  
    int i; 
 
    i = -10;     
    do { 
      if(i > 0) 
         break; 
      Console.Write(i + " "); 
      i++; 
    } while(i <= 10); 
  
    Console.WriteLine("Done");  
  }  
}

The code above generates the following result.

Find the square root of a number

The following code uses the do while statement to implement Newton's method for finding the square root of a number.

using System;// j ava2s. co m

public class MainClass {
   public static void Main( ) {
      double epsilon   = 1.0e-9;
      double guess    = 11.0;
      double result   = 0.0;

      double value = 2;

      result = ((value / guess) + guess) / 2; 

      do {
            Console.WriteLine( "Guess Value  = {0}", guess  );
            Console.WriteLine( "Result Value = {0}", result );
            guess = result;
            result = ((value / guess) + guess) / 2;
      } while( Math.Abs(result - guess) > epsilon );
      Console.WriteLine("The approx sqrt of {0} is {1}", value, result );
    }
}

The code above generates the following result.

Do / while loop with a console read

using System;//from  j  a v a2 s  .  c  o  m
using System.IO;

class MainClass
{
  public static void Main(string[] args)
  {
    string ans;
    do
    {
      Console.Write("Are you done? [yes] [no] : ");
      ans = Console.ReadLine();
    }while(ans != "yes");
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Get to know the for loop syntax
  2. Control variabls in for loop
  3. Omit for loop part
Home » C# Tutorial » Statements
Comments
if Statement
while loop
do...while loop
for loop
foreach
switch
break
continue statement
goto statement
Xml Documentation