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 (...).
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 चाहिए।
अक्सर पूछे जाने वाले प्रश्न (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 क्या है?
while (true)। यह कभी events की प्रतीक्षा करते programs के लिए जानबूझकर होता है, पर आमतौर पर condition वाले variable को update करना भूलने से हुआ bug होता है।आकस्मिक infinite loop कैसे रोकूँ?
break भी इस्तेमाल कर सकते हैं।