YouTip LogoYouTip

Csharp Do While Loop

# C# do...while Loop [![Image 4: C# Loops](#) C# Loops](#) Unlike **for** and **while** loops, which test the loop condition at the beginning of the loop, the **do...while** loop checks its condition at the end of the loop. The **do...while** loop is similar to the while loop, but the do...while loop will ensure that the loop body is executed at least once. ## Syntax The syntax of the **do...while** loop in C#: do{ statement(s);}while( condition ); Please note that the condition expression appears at the end of the loop, so the statement(s) in the loop body will be executed at least once before the condition is tested. If the condition is true, the control flow jumps back to the do statement above, and the statement(s) in the loop are executed again. This process repeats until the given condition becomes false. ## Flowchart ![Image 5: do...while loop in C#](#) ## Example ## Example using System; namespace Loops { class Program { static void Main(string[] args) { /* Local variable definition */ int a =10; /* do loop execution */ do { Console.WriteLine("a Value: {0}", a); a = a +1; }while(a <20); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: a Value: 10 a Value: 11 a Value: 12 a Value: 13 a Value: 14 a Value: 15 a Value: 16 a Value: 17 a Value: 18 a Value: 19 [![Image 6: C# Loops](#) C# Loops](#)
← Csharp Nested LoopsCsharp For Loop β†’