← Back to C# Course | Chapter 4: Control Flow | Lesson 5 of 7

do-while Loop

In this page:

  1. do-while Loop

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

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        int count = 0;

        do
        {
            Console.WriteLine($"Running iteration {count}");
            count++;
        } while (count < 3);
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.