find and solve || findandsolve.com
Please wait.....

What is the use of while loop C#?

A while loop statement in C# repeatedly executes a target statement as long as a given condition is true.

Suppose you want to execute a particular set of statements as long a given condition is met. In such a case, you can use the while loop, which is one of the simplest loops in C#.

In a while loop, the condition is checked at the beginning of the loop. This implies that the set of statements inside the while loop is executed zero or more times, depending on whether the condition is true or false.

The syntax of the while loop is as follows:

while(condition)
       {
          //statements
      }

In the preceding syntax,

 while: Refer to the while keyword that indicates a while loop.
 condition: Refer to the repression that acts as the condition for the while loop.
 statements: Refer to the set of statements that is executed when the condition evaluates to true. The set of statements can be empty, a single statement, or a block of statements. in case of a block of statements, you need to use the curly braces ({}) to enclose the block of statements.

Perform the following steps to understand the use of the while loop:

1. Start Visual Studio 2019
2.Select File->New->Project on the menu bar.The New Project dialog box opens.
3. Select Visual C#->Windows from the Installed Templates section in the left panel of the New Project dialog box
4. Select Console Application from the middle panel to create a Console application.
5. Enter the name of the application as While Loop in the Name text box and the appropriate location for the application in the Location box.
6. Click the OK button to close the New Project dialog box and create the WhileLoop application.
7. Add the code snippet, shown in the listing as given below to the Program.cs file:

using System;
public class Program
{
public static void Main()
{
int i=0;
string[] days={"Sunday","Monday","Tuesday","Wendnesday","Thursday","Firday","Saturday"};
Console.WriteLine("Days of week:");
while(i <=6)
{
Console.WriteLine(days[i]);
i++;
}
Console.ReadLine();
}
}

In the code Listing above, we first declare an int-type variable.I initialize it with the value,0. I variable acts as the loop control variable for the while loop. Then, we create an array of string types, days with the names of the days in a week. The day's array stores the names of the seven days of a week at locations indexed from 0 to 6. After this, we use a while loop to display the names of the days of the week on the console. The while loop first checks whether the current value of the loop control variable, I, is less than or equal to 6. If the condition in the while loop evaluates to true, the name of the day of the week that is stored at the index value, which is the same as the current value of the loop control variable, is displayed on the console. The last statement in the loop body increments the value of the loop control variable by on.

8. Press the F5 key on the keyboard to run the application.


Related information

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If findandsolve.com felt valuable to you, feel free to share it.

1 Comments


Avatar for Selena   Kandakar
Selena Kandakar

Test


Report Response