JS व्हाइल लूप
In this page:
while (condition) {
// loop body
}
do {
// loop body
} while (condition);
while Loop
एक while loop अपना block चलाने से पहले condition जांचता है, और जब तक condition true रहती है तब तक उस block को दोहराता रहता है, और जैसे ही वह false होती है रुक जाता है।
उदाहरण: The while Loop
// Declare the variable `i`, set to `0`
// Declare the variable `i`, set to `0`
let i = 0;
// Keep looping while `i < 3` holds
// Keep looping while `i < 3` holds
while (i < 3) {
// Print `i` to the console
// Print `i` to the console
console.log(i);
i++;
}
do...while Loop
एक do...while loop पहले अपना code block चलाता है, condition जांचने से पहले, जिससे यह गारंटी मिलती है कि block कम से कम एक बार तो चलेगा ही, भले ही condition शुरू से ही false क्यों न हो।
उदाहरण: The do...while Loop
let i = 5;
do {
console.log(i);
i++;
} while (i < 3); // runs once even though condition is false
while बनाम for
जब iterations की संख्या पहले से पता हो तो आमतौर पर for loops को prefer किया जाता है, जबकि while loops उन स्थितियों के लिए बेहतर हैं जहाँ stopping condition किसी ऐसी चीज़ पर निर्भर करती है जिसे सिर्फ loop के दौरान ही जांचा जा सकता है।
उदाहरण: while vs for
// for: iterations known ahead of time
for (let i = 0; i < 3; i++) console.log(i);
// while: stopping point depends on runtime state
let n = 10;
while (n > 1) n = n / 2;
console.log(n);
Infinite Loops से बचना
एक infinite loop तब होता है जब किसी loop की condition कभी false नहीं हो पाती, जिससे code हमेशा के लिए चलता रहता है और आमतौर पर browser tab freeze हो जाता है, loop की update logic को ध्यान से review करना ही सबसे अच्छा बचाव है।
उदाहरण: Avoiding Infinite Loops
// Avoid this - infinite loop, condition never becomes false:
// let i = 0;
// while (i < 5) { console.log(i); }
let i = 0;
while (i < 5) {
console.log(i);
i++; // update makes condition eventually false
}
while कब इस्तेमाल करें
while loops उन स्थितियों में सबसे अच्छे साबित होते हैं जैसे processing के दौरान किसी condition के बदलने का इंतज़ार करना, बार-बार valid input मांगना, या किसी data structure में तब तक काम करना जब तक वह खाली न हो जाए, ऐसे मामले जहाँ stopping point सच में पहले से पता नहीं हो सकता।
उदाहरण: When to Use while
// Declare the variable `queue`, set to `[1, 2, 3]`
// Declare the variable `queue`, set to `[1, 2, 3]`
let queue = [1, 2, 3];
// Keep looping while `queue.length > 0` holds
// Keep looping while `queue.length > 0` holds
while (queue.length > 0) {
// Print `queue.shift()` to the console
// Print `queue.shift()` to the console
console.log(queue.shift());
}
- loop body के अंदर condition variable update करना भूल जाना, जो एक infinite loop बनाता है जो कभी नहीं रुकता।
- while और do...while को confuse करना, do...while हमेशा अपना block कम से कम एक बार चलाता है, भले ही condition शुरुआत में false हो।
- किसी while loop का इस्तेमाल करना जब एक for loop ज़्यादा साफ होता, for आमतौर पर बेहतर है जब आपको पहले से पता हो कि कितनी iterations चाहिए।
- एक while loop हर iteration से पहले अपनी condition check करता है और condition false होते ही रुक जाता है।
- एक do...while loop पहले अपना block एक बार चलाता है, फिर condition check करता है, कम से कम एक execution guarantee करते हुए।
- infinite loops से बचने के लिए careful design चाहिए, हमेशा सुनिश्चित करें कि loop की condition आखिरकार false हो सके।
while और do...while loops core JavaScript syntax हैं जो हर browser और JavaScript environment में समान रूप से supported हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic