s

C# While Loop edit button Edit

author
Rakesh Kumar Sutar | calendar 12 January 2021 | 3279

Introduction

A while loop statement in C# repeatedly executes a target statement as long as a given condition is true. Loops can execute a block of code as long as a specified condition is reached.Loops are handy because they save time, reduce errors, and they make code more readable.

Syntax

while(condition) {
   statement(s);
}

Example - While loop

using System;

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

            /* while loop execution */
            while (a < 20)
            {
                Console.WriteLine("value of a: {0}", a);
                a  ;
            }
            Console.ReadLine();

        }
    }
}

Output