Do-While statement is used to execute the block of statement until the condition is true.
It is similar to while loop but the only difference is that, in while loop the block of statement
will execute only when the condition is true, but in do-while statement will execute at least once, because the body of statement will execute first then the condition is checked.
We use keyword do and while to create the body for do-while loop.
Syntax
do
{
//statement of body to execute
}while(condition);
Example
using System;
namespace Do_While
{
class Program
{
static void Main(string[] args)
{
int i = 1;
do
{
Console.WriteLine("i value: {0}", i);
i++;
} while (i <= 4);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:-
i value: 1 i value: 2
i value: 3
i value: 4
Press Enter Key to Exit..
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Do-While statement is used to execute the block of statement until the condition is true.
It is similar to while loop but the only difference is that, in while loop the block of statement will execute only when the condition is true, but in do-while statement will execute at least once, because the body of statement will execute first then the condition is checked.
We use keyword do and while to create the body for do-while loop.
Syntax
Example
Output:-