While Loop in C#

aaaaaaaaaaaaaaaaa

General form of While Loop


initialise loop counter;
while(test loop counter using a condition)
    {
        do this;
        and this;
        increment loop counter;
    }
Expressions in Loop
Expression Called
++a preincrement
a++ postincrement
--a predecrement
a-- postdecrement

While Loop Printing 0 to 10 Integer

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0; //we assign 0 to a.
            while (a <= 10) //it checking when a less then or equal to 10 then it breaking.
            {                         //while loop starting body.
                Console.WriteLine(a); //first it printing 0 ,again 1 and again 2 and so on.
                a = a + 1; //assigning '0 + 1' to 'a', and again assigning  '1 + 1' to 'a...' and so on.
            } //while loop closing body.      //U also write 'a++' on 'a = a + 1;' its Same.
                Console.ReadKey();
        }
    }
}
Output
Hello
World!

While Loop Average of Class

Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, d;
            a = 0; //assign 0 value to 'a'.
            b = 1; //assign 1 value to 'b'.
            while (b <= 5) //looping from 'b' to 5, it looping 5 times 1 to 5.
            { //loop start body.
                Console.Write("Enter grade in integer:");
                c = Int32.Parse(Console.ReadLine());
                a = a + c; //adding all given values by user and storing in 'a'.
                b = b + 1; //if 'b' value not equal to 5 then it looping and adding 1.
            } //loop closing body.
            d = a / 5; //'a' divide 5, and assign to 'd',it loop is looping 5 times then
                        //use here 5,which number U using in loop use same here.
            Console.WriteLine("\nClass average is {0}",d);
            Console.ReadKey();
        }
    }
}
Output
Hello
World!

While Loop Checking Name like Password for Unlimited Chances
Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {  //if U write 'Please Enter Password' write here its Ur choice.
            String a = null; //I assign 'null' value to 'a', and
            while (a != "Bilal") //while checking if 'a'value not equal to
            { //'Bilal' then do process again and again, when U enter correct
                a = Console.ReadLine(); //'Bilal' then while checking 'a'value
            }    //equal to 'Bilal' then stop loop and printing 'OK Bilal!'.
            Console.WriteLine("OK Bilal!");
            Console.ReadLine();
        }
    }
}
Output
Hello
World!

While Loop Checking Name like Password for 3 Chances
Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            String a;
            while (i <= 2) //when I enter Name 'If statement' checking if 'a'
            { //value not equal to 'Bilal' then adding 1 to 'i' and looping againg and
                Console.Write("Please Enter Password: ");
                a = Console.ReadLine();//again for 3 times, if 'a' value equal to
                if (a == "Bilal") //'Bilal' then it
                {               //printing 'OK Bilal!' and
                    Console.WriteLine("OK Bilal!");
                    break; //'break' Breaking the Statement.
                }
                else
                {
                    Console.WriteLine("Wrong password!");
                }
                i++;
            }
            Console.ReadKey();
        }
    }
}
Output
Hello
World!