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

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples