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

do-while Loop कैसे लिखें

एक do-while loop पहले action करता है और बाद में पूछता है कि दोहराना है या नहीं, जैसे खाने का एक bite लेना और फिर decide करना कि और चाहिए या नहीं। यह हमेशा कम से कम एक बार चलता है।

In this page:

  1. do-while Loop
Syntax
csharp
do
{
    // body runs at least once
} while (condition);

do-while Loop

एक do-while loop अपनी condition body चलाने के *बाद* जांचता है, body के कम से कम एक बार execute होने की गारंटी देते हुए। यह while से अलग है, जो body को पूरी तरह skip कर सकता है।

यह आमतौर पर menu-style loops के लिए उपयोग होता है जिन्हें दोहराना है या नहीं यह जांचने से पहले एक बार चलना चाहिए।

Note: do-while का उपयोग करें जब loop body को कम से कम एक बार चलना चाहिए, जैसे initial user input पढ़ना।

उदाहरण: 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);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. do-while के आखिर में while (condition) के बाद semicolon भूल जाना।
  2. यह भूल जाना कि body हमेशा कम से कम एक बार चलती है।
  3. कभी condition variable न बदलना, एक infinite loop बनाते हुए।
🔒

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.