🟡 Control Flow · Lesson 18
while Loops
while Loops
The while Loop — repeat while a condition holds
let i = 1; // 1. set up the counter yourself
while (i <= 5) { // 2. check the condition
console.log(i);
i++; // 3. update it yourself — DON'T forget!
}
// Output: 1, 2, 3, 4, 5
while repeats a block for as long as its condition stays true. It's the simplest loop — just a condition and a body. But notice the difference from for: a for loop packs the counter setup, condition, and step neatly into one line, whereas with while, YOU must declare the counter before the loop and update it inside the body. That extra responsibility is where bugs creep in: forget the i++ and the condition never becomes false, so the loop runs forever.do...while — always runs at least once
let i = 1;
do {
console.log(i); // runs FIRST...
i++;
} while (i <= 5); // ...THEN checks the condition
// Even with a false condition, the body runs once:
let x = 100;
do {
console.log("This prints once!");
} while (x < 10); // condition is false, but too late
do...while flips the order: it runs the body FIRST, then checks the condition. The consequence is important — the body is guaranteed to run at least once, even if the condition was false from the very start. In a plain while, a false condition means the body never runs at all. Note the semicolon after while (...) at the end — it's required here. Use do...while when you need at least one pass: showing a prompt before validating input, or running a menu once before checking whether to exit.while vs do...while — the key difference
| while | do...while | |
|---|---|---|
| Checks condition | BEFORE the body | AFTER the body |
| Minimum runs | 0 times | 1 time |
| Use when | Might not need to run | Must run at least once |
Everything comes down to WHEN the condition is checked.
while checks first — so if the condition starts false, the body is skipped entirely (zero runs). do...while checks last — so the body always executes at least once before any checking happens. In real code, while is far more common; do...while appears occasionally where a "do it, then see if we should repeat" flow fits naturally. Both are less used than for, which handles most counting tasks.Infinite Loops — the danger
// ✗ DANGER — this never ends! The browser will freeze.
let i = 1;
while (i <= 5) {
console.log(i);
// forgot i++ — the condition stays true forever
}
// ✓ Always make sure something CHANGES toward the condition
while (i <= 5) {
console.log(i);
i++; // eventually i becomes 6, and the loop stops
}
An infinite loop is a loop whose condition never becomes false — it runs forever, freezing the browser tab. This is the classic
while loop bug, caused by forgetting to update the variable in the condition. The rule to internalise: inside every while loop, SOMETHING must change that moves you toward ending the loop. Usually that's incrementing a counter. If your page hangs while testing, an infinite loop is the likely culprit — close the tab and check that your loop variable actually changes. (Press Escape or close the tab to recover.)When to Use while Instead of for
// Use FOR when you know the number of repetitions:
for (let i = 0; i < arr.length; i++) { } // exactly arr.length times
// Use WHILE when you DON'T know how many times:
let guess = getGuess();
while (guess !== secretNumber) { // could be 1 try or 50
guess = getGuess();
}
// Or when looping until some state changes:
while (!isGameOver) { playRound(); }
The choice is about whether you know the count in advance. If you're counting a known number of times — through an array, from 1 to 10 —
for is the natural fit, keeping all the loop machinery in one tidy line. If you're repeating until something happens, and you can't say how many passes that'll take — waiting for correct input, playing until the game ends, reading until data runs out — while expresses that intent perfectly. Both can technically do either job, but choosing the right one makes your code clearer.Exam Corner
Q: When does a while loop check its condition? Before each pass — so it may run zero times.
Q: How is do...while different? It checks the condition AFTER the body, so the body always runs at least once.
Q: What causes an infinite loop? The condition never becoming false — usually forgetting to update the loop variable.
Q: When should you prefer for over while? When you know how many times you'll repeat (e.g. array length).
Q: What must every while loop contain? Something that changes each pass, moving toward ending the loop.
Q: How is do...while different? It checks the condition AFTER the body, so the body always runs at least once.
Q: What causes an infinite loop? The condition never becoming false — usually forgetting to update the loop variable.
Q: When should you prefer for over while? When you know how many times you'll repeat (e.g. array length).
Q: What must every while loop contain? Something that changes each pass, moving toward ending the loop.
while Loop — condition सही रहने तक दोहराओ
let i = 1; // 1. counter khud set karo
while (i <= 5) { // 2. condition jaancho
console.log(i);
i++; // 3. khud update karo — MAT bhoolo!
}
// Output: 1, 2, 3, 4, 5
while block को तब तक दोहराता है जब तक उसकी condition true रहे. यह सबसे सरल loop है — बस एक condition और body. पर for से फर्क देखिए: for loop counter setup, condition, और step को साफ-सुथरा एक line में पैक करता है, जबकि while के साथ, AAPKO loop से पहले counter declare करना और body के अंदर update करना पड़ता है. वह अतिरिक्त ज़िम्मेदारी वहीं है जहां bugs घुसते हैं: i++ भूलिए और condition कभी false नहीं होती, तो loop हमेशा चलता रहता है.do...while — कम से कम एक बार हमेशा चलता है
let i = 1;
do {
console.log(i); // PEHLE chalta hai...
i++;
} while (i <= 5); // ...PHIR condition jaanchta hai
// False condition ke saath bhi, body ek baar chalti hai:
let x = 100;
do {
console.log("This prints once!");
} while (x < 10); // condition false hai, par bahut der ho gayi
do...while क्रम उलटता है: यह PEHLE body चलाता है, फिर condition जांचता है. नतीजा अहम है — body कम से कम एक बार चलने की गारंटी है, भले ही condition बिल्कुल शुरू से false हो. साधारण while में, false condition मतलब body बिल्कुल नहीं चलती. अंत में while (...) के बाद semicolon ध्यान दीजिए — यहां यह ज़रूरी है. do...while तब use कीजिए जब कम से कम एक pass चाहिए: input validate करने से पहले prompt दिखाना, या exit जांचने से पहले menu एक बार चलाना.while vs do...while — मुख्य अंतर
| while | do...while | |
|---|---|---|
| Condition जांचता | Body से PEHLE | Body के BAAD |
| न्यूनतम runs | 0 बार | 1 बार |
| तब use करें | चलने की ज़रूरत न हो शायद | कम से कम एक बार चलना चाहिए |
सब कुछ इस पर आता है कि condition KAB जांची जाती है.
while पहले जांचता है — तो अगर condition शुरू में false हो, body पूरी तरह छूट जाती है (शून्य runs). do...while आखिर में जांचता है — तो कोई भी जांच होने से पहले body हमेशा कम से कम एक बार चलती है. असली code में, while कहीं ज़्यादा common है; do...while कभी-कभार आता है जहां "करो, फिर देखो दोहराना चाहिए" प्रवाह स्वाभाविक बैठे. दोनों for से कम use होते हैं, जो ज़्यादातर गिनती के काम संभालता है.Infinite Loops — खतरा
// ✗ KHATRA — yeh kabhi khatm nahi hota! Browser jam ho jayega.
let i = 1;
while (i <= 5) {
console.log(i);
// i++ bhool gaye — condition hamesha true rehti hai
}
// ✓ Hamesha pakka karo kuch condition ki taraf BADLE
while (i <= 5) {
console.log(i);
i++; // aakhirkar i 6 banta hai, aur loop rukta hai
}
Infinite loop वह loop है जिसकी condition कभी false नहीं होती — यह हमेशा चलता है, browser tab जमा देते. यह classic
while loop bug है, condition के variable को update करना भूलने से. आत्मसात करने का rule: हर while loop के अंदर, KUCH बदलना चाहिए जो आपको loop खत्म करने की ओर ले जाए. आमतौर पर वह counter बढ़ाना है. अगर testing के दौरान आपका page अटके, infinite loop संभावित अपराधी है — tab बंद कीजिए और जांचिए आपका loop variable असल में बदलता है. (उबरने को Escape दबाइए या tab बंद कीजिए.)for के बजाय while कब use करें
// FOR tab jab repetitions ki sankhya pata ho:
for (let i = 0; i < arr.length; i++) { } // theek arr.length baar
// WHILE tab jab NA pata ho kitni baar:
let guess = getGuess();
while (guess !== secretNumber) { // 1 try ya 50 ho sakte
guess = getGuess();
}
// Ya jab kisi state ke badalne tak loop karna ho:
while (!isGameOver) { playRound(); }
चुनाव इस पर है कि आपको गिनती पहले से पता है या नहीं. अगर आप ज्ञात संख्या में गिन रहे हैं — array पर, 1 से 10 तक —
for स्वाभाविक फिट है, सारी loop machinery एक सुथरी line में रखते. अगर आप कुछ होने तक दोहरा रहे हैं, और नहीं कह सकते कितने passes लगेंगे — सही input का इंतज़ार, game खत्म होने तक खेलना, data खत्म होने तक पढ़ना — while वह इरादा perfectly व्यक्त करता है. तकनीकी रूप से दोनों कोई भी काम कर सकते हैं, पर सही चुनना आपका code साफ बनाता है.Exam Corner
Q: while loop अपनी condition कब जांचता है? हर pass से पहले — तो यह शून्य बार भी चल सकता है.
Q: do...while कैसे अलग है? यह condition body के BAAD जांचता है, तो body हमेशा कम से कम एक बार चलती है.
Q: Infinite loop किससे होता है? Condition कभी false न होने से — आमतौर पर loop variable update करना भूलने से.
Q: while पर for कब पसंद करें? जब पता हो कितनी बार दोहराएंगे (जैसे array length).
Q: हर while loop में क्या होना चाहिए? कुछ जो हर pass बदले, loop खत्म करने की ओर ले जाते.
Q: do...while कैसे अलग है? यह condition body के BAAD जांचता है, तो body हमेशा कम से कम एक बार चलती है.
Q: Infinite loop किससे होता है? Condition कभी false न होने से — आमतौर पर loop variable update करना भूलने से.
Q: while पर for कब पसंद करें? जब पता हो कितनी बार दोहराएंगे (जैसे array length).
Q: हर while loop में क्या होना चाहिए? कुछ जो हर pass बदले, loop खत्म करने की ओर ले जाते.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.