Using do Statements in C#
The while and for statements both test their Boolean expression at the start of the loop. This means that if the expression evaluates to false on the first test, the body of the loop does not run, not even once. The do statement is different; its Boolean expression is evaluated after each iteration, so the body always executes at least once.
Syntax:-
do
statements
while (booleanExpression);
We must use a statement block if the body of the loop comprises more than one statement. Here’s a version of the example that writes the values 0 through 9 to the console, this time constructed using a do statement:
int i = 0;
do
{
Console.WriteLine(i);
i++;
}
while (i < 10);
Comments
Post a Comment