🟡 Control Flow  ·  Lesson 18

while and do-while Loops

What is a loop?

A loop repeats a block of code while a condition stays true, so you do not write the same lines over and over. C++ has three loops; this lesson covers while and do-while. The for loop is covered separately.

The while loop

A while loop checks the condition first, then runs the body if it is true — repeating until the condition becomes false:

C++
#include <iostream>
using namespace std;
int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << " ";
        i++;               // update, or it loops forever
    }
    return 0;
}
Output:
1 2 3 4 5

The do-while loop

A do-while loop runs the body first, then checks the condition — so the body always runs at least once:

C++
int i = 1;
do {
    cout << i << " ";
    i++;
} while (i <= 5);
Output:
1 2 3 4 5

Note the semicolon after while (...) in a do-while — it is required.

while vs do-while

The difference shows when the condition is false from the start:

C++
int n = 10;
while (n < 5) { cout << "while\n"; }      // prints nothing

do { cout << "do-while\n"; } while (n < 5); // prints once
Output:
do-while
whiledo-while
Condition checkedBefore bodyAfter body
Minimum runs01
Best forMaybe-zero repetitionMenus, input validation

Infinite loops

If the condition never turns false, the loop runs forever. This is usually a bug — often a forgotten update:

C++
int i = 1;
while (i <= 5) {
    cout << i << " ";
    // forgot i++  → infinite loop!
}

Intentional infinite loops (like while (true)) are used with a break inside to exit when needed.

Common mistakes

  • Forgetting to update the loop variable, causing an infinite loop.
  • Leaving out the semicolon after while (...) in a do-while.
  • Using = instead of == or the wrong comparison in the condition.
  • An off-by-one error — printing one too many or too few times.
🏋️ Practice

Use a while loop to print the sum of numbers from 1 to a value the user enters. Then write a menu with do-while that repeats until the user chooses "Exit".

Summary

  • Loops repeat code while a condition stays true.
  • while checks the condition before the body (may run zero times).
  • do-while checks after the body (runs at least once).
  • Always update the condition variable to avoid infinite loops.
  • do-while needs a semicolon after its while (...).

Loop क्या है?

Loop एक code block को तब तक दोहराता है जब तक कोई condition true रहे, तो आपको वही lines बार-बार नहीं लिखनी पड़तीं। C++ में तीन loops हैं; यह lesson while और do-while कवर करता है। for loop अलग से कवर है।

while loop

while loop पहले condition जाँचता है, फिर true होने पर body चलाता है — condition false होने तक दोहराते हुए:

C++
#include <iostream>
using namespace std;
int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << " ";
        i++;               // update, warna hamesha chalega
    }
    return 0;
}
Output:
1 2 3 4 5

do-while loop

do-while loop पहले body चलाता है, फिर condition जाँचता है — तो body हमेशा कम-से-कम एक बार चलती है:

C++
int i = 1;
do {
    cout << i << " ";
    i++;
} while (i <= 5);
Output:
1 2 3 4 5

do-while में while (...) के बाद semicolon पर ध्यान दें — यह ज़रूरी है।

while बनाम do-while

अंतर तब दिखता है जब condition शुरू से ही false हो:

C++
int n = 10;
while (n < 5) { cout << "while\n"; }      // kuchh print nahi

do { cout << "do-while\n"; } while (n < 5); // ek baar print
Output:
do-while
whiledo-while
Condition कब जाँचाBody से पहलेBody के बाद
न्यूनतम runs01
किसके लिएशायद-शून्य दोहरावMenus, input validation

Infinite loops

अगर condition कभी false न हो, loop हमेशा चलता है। यह आमतौर पर bug है — अक्सर भूला हुआ update:

C++
int i = 1;
while (i <= 5) {
    cout << i << " ";
    // i++ bhool gaye  → infinite loop!
}

जानबूझकर infinite loops (जैसे while (true)) अंदर एक break के साथ इस्तेमाल होते हैं ताकि ज़रूरत पड़ने पर बाहर निकला जा सके।

आम गलतियाँ

  • Loop variable update करना भूलना, infinite loop पैदा करना।
  • do-while में while (...) के बाद semicolon छोड़ देना।
  • Condition में == की जगह = या गलत तुलना इस्तेमाल करना।
  • Off-by-one गलती — एक बार ज़्यादा या कम print करना।
🏋️ अभ्यास

while loop से 1 से user के दिए value तक numbers का योग print करें। फिर do-while से एक menu लिखें जो user के "Exit" चुनने तक दोहराए।

सारांश

  • Loops code को तब तक दोहराते हैं जब तक condition true रहे।
  • while body से पहले condition जाँचता है (शून्य बार चल सकता है)।
  • do-while body के बाद जाँचता है (कम-से-कम एक बार चलता है)।
  • Infinite loops से बचने को हमेशा condition variable update करें।
  • do-while को अपने while (...) के बाद semicolon चाहिए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में while और do-while में क्या अंतर है?
while loop अपनी condition body चलाने से पहले जाँचता है, तो शुरू में condition false हो तो body कभी नहीं चलती। do-while loop condition body चलाने के बाद जाँचता है, तो body हमेशा कम-से-कम एक बार चलती है। वही निश्चित पहला run मुख्य अंतर है।
C++ में while loop कब इस्तेमाल करूँ?
while loop तब इस्तेमाल करें जब पहले से पता न हो कि कितनी बार दोहराना है और किसी condition के बदलने तक चलते रहना चाहें — जैसे user के sentinel value टाइप करने तक input पढ़ना। यह गिनने के बजाय condition-चालित दोहराव के लिए आदर्श है।
क्या do-while loop हमेशा कम-से-कम एक बार चलता है?
हाँ। चूँकि do-while loop अपनी condition body execute करने के बाद ही जाँचता है, body कम-से-कम एक बार चलती है भले condition शुरू से ही false हो। यह इसे menus और input validation के लिए आदर्श बनाता है।
C++ में infinite loop क्या है?
Infinite loop वह है जिसकी condition कभी false नहीं होती, तो वह हमेशा चलता है — जैसे while (true)। यह कभी events की प्रतीक्षा करते programs के लिए जानबूझकर होता है, पर आमतौर पर condition वाले variable को update करना भूलने से हुआ bug होता है।
आकस्मिक infinite loop कैसे रोकूँ?
पक्का करें कि loop के अंदर कुछ उस variable को बदले जो condition में इस्तेमाल होता है ताकि वह अंततः false हो जाए। Counter बढ़ाना या flag update करना भूलना सबसे आम कारण है; आप किसी condition के पूरे होने पर loop छोड़ने को break भी इस्तेमाल कर सकते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.