In this article I am going to discuss about C# While Loop but before that if you had not read my previous article of C# For Loop then must read it for clear understanding about loops and iteration statements. C# While loop is another type of iteration statements under control flow statements. It is also used to iterate over the part of program several times to execute one code so many times. Mainly the work of all the loops are same but the only difference is with their syntax and names.

Introduction to C# While Loop

In C#, While Loop is used to executing a statement continuously till the condition is not failed. In simple words while loop is used to execute piece of code as long as the condition is true. While loop can be performed using while keyword. If the condition has failed then it will come out of loop and execution stops. It is one of the alternatives of C# for loop. The syntax for While Loop Statement is given below :

while(condition){
 //Statement
}

C# Program to Explain While Loop

using System;
namespace CsharpWhileLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 1;
            while (number <= 5)
            {
                Console.WriteLine("Number is : " + number);
                number++;
            }
            Console.ReadKey();
        }
    }
}
Output:
Number is : 1
Number is : 2
Number is : 3
Number is : 4
Number is : 5

Above is the simple program that shows the working of while loop. First we have declared one iteration variable i and then we have used the keyword while for using while loop and then we checking the condition. If the condition is true then it will execute the block of code and also increment the iteration variable. But if the condition is fail then it will terminate the loop and came out of loop.

Entry Controlled Loop

While loop is also known as entry controlled loop because before evaluating the piece of code , the conditional part will be executed. Therefore it is normal to say if we are working with while loop then pre-checking process will occur. In simple words the loop in which before executing the body of the loop if the condition is tested first then it is called an entry-controlled loop.

Nested While Loop

We have already discussed the meaning of nesting in our previous articles but if have not read that articles then I going to tell here. In programming nesting means using another inside one loop. For example I am made one program which contains one loop to execute the body and inside that loop I have again made one more loop then it is called nested loops. In C# like nested for loops we can use nested while loops to perform an action. This is the main reason to say nested loops are called loops inside the loop. In nested while loop, we write another while loop inside the first while loop. It will be more clear with an program.

C# Program to Explain Nested While Loops

using System;
namespace NestedWhileLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number : ");
            int number = Convert.ToInt32(Console.ReadLine());
            int i = 1;
            while (i <= number)
            {
                Console.WriteLine();
                int j = 1;
                while (j <= i)
                {
                    Console.Write(j + " ");
                    j++;
                }
                i++;
            }

            Console.ReadKey();
        }
    }
}
Output:
Enter a number : 6

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 

In above program we are taking number as input from user and making while loop and checking the condition that i must be less than equal to number. If the condition is true then it enter inside the body and Inside body we are again writing one inner while loop and checking that j must be less than equal to i. If yes, then it will execute the body otherwise it will simple came out of inner loop and increment the value of i. Then again outer loop checks condition and executing like the same. For more clear understanding I must suggest you to make this program by own and try to understand this with writing each and every iteration over notebook and then run this program.

Conclusion

In this C# tutorials article I have discussed about C# While loop and nested while loop with programs. Always remember programming is not the thing that you can learn from just reading articles and watching tutorials. Therefore after understanding the concept try to implement the program in your notebook and then run it in the compiler. This will not only increase your coding skills but also helps in brainstorming. Hope you like this article and understand while loop in csharp. In my next article I going to explain do-while loop in csharp programming.