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

Welcome back.






Before Read Terms of use

How to Use Do Loop in C#


As you know,the condition in a while loop is checked at the beginning of the loop meaning that the loop can execute zero or more number of times. This implies if the condition in the while loop is false in the first iteration itself,then the while loop is not executed at all. However,there can be situations when you want to execute a set of statements at least once,for instance,when you want to display the initial bidding price for an auction.In such situations,you can use the do loop.

In the do loop,the set of statements appear before the condition,which is checked at the end of the loop.This implies that first the set of statements is executed and then the condition is checked.If the condition is true,then control goes to the beginning of the loop executing the set of statements again.The do loop executes as long as the condition is true. When the condition becomes false,the do loop terminates and control goes to the statement following the do loop.Therefore,the set of statements in a do loop is executed at least once irrespective of the value of the condition.

The syntax of the do loop is as follows:

do
 {
   //statements
}
while(condition);

In the preceding syntax,
do : Refers to the do keyword that indicates the start of the do loop.
statements: Refers to the set of statements that is executed as a part of the do loop.
while: Refers to the while keyword that indicates the while statement in the do loop.
condition: Refers to the expression that is used as a condition for the do loop.

Perform the following steps to understand the use of the do 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 DoLoop in the Name text box and an appropriate location for the application in the Location box.
6.Click the Ok button to close the New Project dialog box and crate the DoLoop application.
7.Add the code snippet,shown as given below ,to the Program.cs File:


using System;
public class Program
{
public static void Main()
{
int i=0;
int[] numbers={24,13,17,34,55,86};
Console.WriteLine("Array elements:");
do
{
if(numbers[i] %2==0)
{
Console.WriteLine(numbers[i]+ ":even number");
}
else
{
Console.WriteLine(numbers[i]+":odd number");
}
i++;
}
while(i <=5);
Console.ReadLine();
}
}

In the code as 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 do loop.Then,we create an array of int type,numbers,with a set of integer values.The numbers array store the integer values at locations indexed from 0 to 5.After this,we use a do loop to display the array elements on the console.Inside the do loop,we use an if statement to check whether the current array element is an even number or odd number and display the element on the console marking it as even or odd.The loop control variable,i,is used to determine the current array element.The last statement in the loop body increments the value of the loop control variable by one.At the end of the loop,the current value of the loop control variable is checked whether it is less the or equal to 5.

8.Press the F5 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.

Comments



Report Response