In this tutorial, while loop in C#. We have already discussed for loop in our previous tutorial. The while loop is another statement in C#.

After completing this tutorial you will be able to understand:

  • while loop in C#.
  • how to use while loop in c#.

Introduction:

While Loop is used to execute a block of code as long as the specified condition is true

The syntax for declaring While Loop:

While(condition)

{



// statement



}

In the above syntax, the statement may be a single line statement or block of statement. The statement executed until the condition is true.

while loop

The interesting fact about while loop that the stamen might not ever run. When the condition is tested.

An example of the while loop in C#.

Open visual studio and create a Console Application project in C# called WhileLoopDemo.

whole loop

Open Program.cs file and write the following code.

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



namespace WhileLoopDemo
{
class Program
{
static void Main(string[] args)
{
int iCount=1;
Console.WriteLine("Enter the Number for which you want to print table");
int iNumber = Convert.ToInt32(Console.ReadLine());
while (iCount <= 10)
{
Console.WriteLine("{0} X {1} = {2}", iNumber, iCount, iNumber * iCount);
iCount++;
}
Console.ReadKey();



}
}
}

Run the program and look over the output window.

while loop

Conclusion:

Hope you understand this small but very important tutorial about while loop in C#. I would like to have your feedback, comments and suggestions.

View More:

Thank You.

Leave a Comment