do-while Loop कैसे लिखें
एक do-while loop पहले action करता है और बाद में पूछता है कि दोहराना है या नहीं, जैसे खाने का एक bite लेना और फिर decide करना कि और चाहिए या नहीं। यह हमेशा कम से कम एक बार चलता है।
In this page:
Syntax
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
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
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. #}
आम गलतियां
- do-while के आखिर में
while (condition)के बाद semicolon भूल जाना। - यह भूल जाना कि body हमेशा कम से कम एक बार चलती है।
- कभी condition variable न बदलना, एक infinite loop बनाते हुए।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: