How to use C# do while loop?

CsharpProgrammingServer Side Programming

The do...while loop checks its condition at the end of the loop. It is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

To create a do while loop −

do {
   statement(s);
} while( condition );

The conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.

The following is an example −

Example

 Live Demo

using System;

namespace Loops {
   class Program {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 50;

         /* do loop execution */
         do {
            Console.WriteLine("value of a: {0}", a);
            a = a + 1;
         }

         while (a < 20);
         Console.ReadLine();
      }
   }
}

Output

value of a: 50
raja
Published on 08-Aug-2018 10:50:36
Advertisements