🟡 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 (...).

Frequently Asked Questions

What is the difference between while and do-while in C++?
A while loop checks its condition before running the body, so if the condition is false at the start the body never runs. A do-while loop checks the condition after running the body, so the body always runs at least once. That guaranteed first run is the key difference.
When should I use a while loop in C++?
Use a while loop when you do not know in advance how many times to repeat and want to keep going until a condition changes — such as reading input until the user types a sentinel value. It is ideal for condition-driven repetition rather than counting.
Does a do-while loop always run at least once?
Yes. Because a do-while loop tests its condition only after executing the body, the body runs at least one time even if the condition is false from the very start. This makes it ideal for menus and input validation.
What is an infinite loop in C++?
An infinite loop is one whose condition never becomes false, so it runs forever — such as while (true). It is sometimes intentional for programs that wait for events, but usually it is a bug caused by forgetting to update the variable in the condition.
How do I stop an accidental infinite loop?
Make sure something inside the loop changes the variable used in the condition so it eventually becomes false. Forgetting to increment a counter or update a flag is the most common cause; you can also use break to leave a loop when a certain condition is met.

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 चाहिए।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.