While loop

While loop is a programming loop which repeatedly execute a block of code as long as the specified condition returns True. The while loop starts with the while keyword, and it must include a boolean conditional expression inside brackets that returns either true or false. It executes the code block until the specified conditional expression returns false.

Before writing any loop be clear with your goal. If goal is not clear writing loop will be very difficult. Once the goal is clear ask yourself whats the starting point and termination point of the loop. Starting point and termination point is easy to figure out if goal is clear.

CODE

EG 1 . Printing 1 to 5 using While loop

static void Main(string[] args) {
    int i = 1;
    while (i<=5) {
        Console.WriteLine(i);
        i++;
    }
}



EG 2 . Printing 5 to 1 using While loop

static void Main(string[] args) {
    int i = 5;
    while (i>=1) {
        Console.WriteLine(i);
        i--;
    }
}




(Click Here, For more exercises)