All Type Coding

Search Here

while loop in c#

while loop
                        A while loop statement executes a statement or a block of statements until a specified expression evaluates to false . In some situation you may want to execute the loop at least one time and then check the condition.

Syntax
while(condition)
{
   statement(s);

}

























For Example :

using System;
class Program
{
    static void Main(string[] args)
    {
      
        int a = 10;
    
        while (a <= 20)
        {
            Console.WriteLine("value of a: {0}", a);
            a++;
        }
        Console.ReadLine();
    }

}

Output will be:











In the above example, three things are important.
(i) Initialization
(ii) Increment/Decrement
(iii) Termination


These are the important characteristics of any loop. Initialization represents the starting position from there your loop will be started. In the above example int a=10 refers to initialization of while loop. a++ refers to increment/decrement and finally while(a<=20) refers to the termination of while loop.

No comments :

Post a Comment