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

Welcome back.






Before Read Terms of use

Using while Loop in C#



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 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 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 of Listing above,we first declare an int type variable.i,and initialize it with the value,0.The i variable acts as the loop control variable for the while loop.Then,we create an array of string type,days with the names of the days in a week.The days 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 a 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 the name of the day of the week that is stored at the index value,which is same as the current value of the loop control variable,is displayed on the console.Thee 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.


Sundar  Neupane

Sundar Neupane

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

1 Comments


Avatar for Selena   Kandakar
Selena Kandakar

Test


Report Response