How to use while Loop in C#, while loop c#, C# while loop, while loop in c#
Hello friends, I hope you all are great. Today, I am posting 10th tutorial in C# series and its about How to use while Loop in C#. It's gonna be a quick tutorial, as there's not much to discuss. In our 8th tutorial in C# series, we have had a look at How to use IF Loop in C# and we have seen that IF loop takes a Boolean expression and if it's TRUE then it allows the compiler to enter in it just once. While loop is quite similar to IF loop as it takes a Boolean expression as well but it will keep on executing again & again, so let's have a look at it in detail:

How to use while Loop in C#

  • While Loop in C# takes a Boolean expression as a condition and it will keep on executing as long as the conditional expression returns true, we can say Execute while condition is TRUE.
  • Here's the syntax of while loop:
while (Boolean Expression) { // Code will execute, while Boolean Expression is TRUE }
  • So, let's design a simple example to understand How while Loop works:
How to use while Loop in C#, while loop c#, C# while loop, while loop in c#
  • In schools, you must have studied math tables, so I have created a simple code using while loop which asks from the user to enter table of.
  • When user enters the number, the program will print out its table till 10.
  • If you look at the code, I have used while loop and this while loop will execute 10 times as the condition in while loop is TotalLength <= 10.
  • I have incremented this TotalLength variable in while Loop so when it will become 10, the while loop will stop.
  • So, instead of writing 10 lines to display table, we are just using while loop, we can also increase the lines to 20 or 30 quite easily, that's the benefit of while loop.
  • Let's play with the code a little and print values till 20 and also use IF Loop in this while loop, which we have studied in 8th lecture.
How to use while Loop in C#, while loop c#, C# while loop, while loop in c#
  • You can see in above code that I have increased the length to 20 and then in while Loop, I have used IF Loop.
  • IF the length is 10 then I am just adding blank lines to separate first 10 from last 10.
  • I hope you got the idea of How to use While Loop and what's its difference from IF Loop, the IF Loop executed just once when condition comes true.
  • Here's the complete code used in today's lecture:
using System;

namespace TEPProject
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.Write("Enter Table of : ");
            int TableOf = int.Parse(Console.ReadLine());

            int TotalLength = 1;

            while (TotalLength <= 20)
            {
                int TableValue = TotalLength * TableOf;
                Console.WriteLine("{0} x {1} = {2}", TableOf, TotalLength, TableValue);
                if(TotalLength==10)
                {
                    Console.WriteLine("\n");
                }
                TotalLength++;
            }
            Console.WriteLine("\n\n");
            
        }
    }
}
So, that was all about How to use while Loop in C#. In our coming tutorial, we will have a loop at How to use do While Loop in C#. Till then take care !!! :)