How To Use Do While Loop In C#

In this article, I will explain how the Do..While Loop can be used in C#.
  • 2681

Introduction

The do statement executes a statement or a block of statements enclosed in {} repeatedly until a condition has been reached. In the following example the do-while loop statements execute as long as the variable r is less than 10. The syntax for the do ... while loop is as follows

Syntax

do

{

 // C# statements here

} while (''conditional expression'')

 

Example

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

class WhileTest

{

    static void Main()

    {

        int r = 1;

        int s = 10;

        int sum = 0;

        do

        {

            sum = r + s;

            Console.WriteLine(" value of sum is {0}", sum);

            r++;

        }

        while (r < 10);

        Console.ReadLine();

    }

 

The output of following program


Clipboard110.jpg

© 2020 DotNetHeaven. All rights reserved.