Home » C# Basics » 03 - C# Programming - Part 1
3

Do While Loop

Defines what the do-while-loop is and the conditions of its use in a C# program

The `do-while' loop is a bit different from the other two loop statements we have seen. Let us check its syntax to understand the difference.

initialize loop;
do
{
statements of the loop;
}
while(check condition);

As we can see, there is no condition checking before the loop starts. So, the loop will run at least once irrespective of the initial value and the testing condition. Consider the following example:

int count;
count=20;
do
{
//some statements
}
while(count<10);

In this example, the initial value of count is 20. Since there is no check condition before the program enters the loop, the statements inside the loop will execute. After this, the condition will be checked. Since 20 is not less than 10, the condition will become false and the loop will stop. Compare this with the `for' and `while' loops. They will not execute a single time if the initial value makes the condition false.

In this chapter, we have learned the basics of the C# programming language. The next chapter will introduce some advanced concepts of C#.