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:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
i++; // update, or it loops forever
}
return 0;
}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:
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);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:
int n = 10;
while (n < 5) { cout << "while\n"; } // prints nothing
do { cout << "do-while\n"; } while (n < 5); // prints oncedo-while
| while | do-while | |
|---|---|---|
| Condition checked | Before body | After body |
| Minimum runs | 0 | 1 |
| Best for | Maybe-zero repetition | Menus, input validation |
Infinite loops
If the condition never turns false, the loop runs forever. This is usually a bug — often a forgotten update:
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.
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.
whilechecks the condition before the body (may run zero times).do-whilechecks 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++?
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++?
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?
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++?
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?
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 होने तक दोहराते हुए:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
i++; // update, warna hamesha chalega
}
return 0;
}1 2 3 4 5
do-while loop
do-while loop पहले body चलाता है, फिर condition जाँचता है — तो body हमेशा कम-से-कम एक बार चलती है:
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);1 2 3 4 5
do-while में while (...) के बाद semicolon पर ध्यान दें — यह ज़रूरी है।
while बनाम do-while
अंतर तब दिखता है जब condition शुरू से ही false हो:
int n = 10;
while (n < 5) { cout << "while\n"; } // kuchh print nahi
do { cout << "do-while\n"; } while (n < 5); // ek baar printdo-while
| while | do-while | |
|---|---|---|
| Condition कब जाँचा | Body से पहले | Body के बाद |
| न्यूनतम runs | 0 | 1 |
| किसके लिए | शायद-शून्य दोहराव | Menus, input validation |
Infinite loops
अगर condition कभी false न हो, loop हमेशा चलता है। यह आमतौर पर bug है — अक्सर भूला हुआ update:
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 रहे।
whilebody से पहले condition जाँचता है (शून्य बार चल सकता है)।do-whilebody के बाद जाँचता है (कम-से-कम एक बार चलता है)।- Infinite loops से बचने को हमेशा condition variable update करें।
- do-while को अपने
while (...)के बाद semicolon चाहिए।