How to use While Loop in C#

In this article, I will explain how while Loop can be used in C#.
  • 2445

Introduction

While statement is a Iterative statements repeat a particular statement block until a condition has been reached. The statement block of while statement is executed while the Boolean expression is true. The statement block is not executed if the Boolean expression is initially false. The syntax for the while loop is as follows.

Syntax

while (''condition'')

{

      // C# statements go here

}

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;

        while (r < 10)

        {

             sum = r + s ;

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

            r++;

        }

            Console.ReadLine();

    }

}

The output of following program

Clipboard110.jpg

© 2019 DotNetHeaven. All rights reserved.