do-while Loop
In this page:
do-while Loop
A do-while loop checks its condition *after* running the body, guaranteeing the body executes at least once. This differs from while, which may skip the body entirely. It's commonly used for menu-style loops that must run once before checking whether to repeat.
Note: Use do-while when the loop body must run at least one time, like reading initial user input.
Example: do-while Loop
using System;
class Program
{
static void Main(string[] args)
{
int count = 0;
do
{
Console.WriteLine($"Running iteration {count}");
count++;
} while (count < 3);
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: