Control Flow

Do While Loop in C#

The do-while loop executes a block of code while a boolean expression evaluates to true. It checks the boolean expression after each iteration, guaranteeing at least one iteration. It terminates as soon as the expression evaluates to false.


Syntax
do {  
   //statements 
} while(booleanExpression);

Notes

booleanExpression results in either a true or false output. It is created using comparing operators (==, >, =, <=, !=).

There can also be multiple boolean expressions within the parentheses (booleanExpression). The boolean expressions are connected through logical operators (&&, ||, !).

If the boolean expression result is true, the statements within the structure will be executed. They will continue to loop through the structure until the booleanExpression is false.


Example
int x = 0; 
do { 
   Console.WriteLine(x); 
   x++; 
} while(x < 10);

Add to Slack

< While Loop   |   Try Catch Block >

© 2019 SyntaxDB. All Rights Reserved.