while Loop का उपयोग
while एक code block को सिर्फ तब तक दोहराता है जब तक एक condition true रहे।In this page:
while condition {
// body
}
Basic while Loop
एक while loop हर iteration से पहले अपना boolean condition जांचता है और उस condition के false होते ही रुक जाता है। जब iterations की संख्या पहले से पता न हो तो यह आदर्श है।
उदाहरण: Basic while Loop
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `n`, initialized to `3`
let mut n = 3;
// Keep looping while `n > 0` holds
while n > 0 {
// Print "{}" to the console, substituting in `n` (with a trailing newline)
println!("{}", n);
n -= 1;
}
// Print "Liftoff!" to the console (with a trailing newline)
println!("Liftoff!");
}
Login to try C/C++/Java/PHP code in the editor
while with a Complex Condition
एक while loop में condition && और || जैसे logical operators उपयोग करके कई checks combine कर सकता है, loop को कई बदलते state pieces पर respond करने देते हुए।
उदाहरण: while with a Complex Condition
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `x`, initialized to `0`
let mut x = 0;
// Declare a mutable variable `y`, initialized to `10`
let mut y = 10;
// Keep looping while `x < 5 && y > 0` holds
while x < 5 && y > 0 {
x += 1;
y -= 2;
}
// Print "x = {}, y = {}" to the console, substituting in `x, y` (with a trailing newline)
println!("x = {}, y = {}", x, y);
}
Login to try C/C++/Java/PHP code in the editor
Accumulating with while
while loops आमतौर पर iterations में एक result accumulate करने के लिए उपयोग होते हैं, जैसे values का sum लेना, जब तक कोई stopping condition reach न हो।
उदाहरण: Accumulating with while
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `sum`, initialized to `0`
let mut sum = 0;
// Declare a mutable variable `i`, initialized to `1`
let mut i = 1;
// Keep looping while `i <= 5` holds
while i <= 5 {
sum += i;
i += 1;
}
// Print "Sum 1 to 5: {}" to the console, substituting in `sum` (with a trailing newline)
println!("Sum 1 to 5: {}", sum);
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Infinite Loops
चूंकि while यह जानने के लिए पूरी तरह अपने condition पर निर्भर है कि कब रुकना है, यह सुनिश्चित करना आपकी ज़िम्मेदारी है कि loop body अंततः उस condition को false बना दे। यह भूल जाने से एक program बनता है जो हमेशा के लिए चलता है।
उदाहरण: Avoiding Infinite Loops
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `countdown`, initialized to `3`
let mut countdown = 3;
// Keep looping while `countdown != 0` holds
while countdown != 0 {
// Print "{}..." to the console, substituting in `countdown` (with a trailing newline)
println!("{}...", countdown);
countdown -= 1;
}
// Print "Done!" to the console (with a trailing newline)
println!("Done!");
}
Login to try C/C++/Java/PHP code in the editor
- Loop की condition variable को body के अंदर update करना भूल जाना, एक infinite loop का कारण बनते हुए।
whileउपयोग करना जहां एक range या iterator पर एकforloop ज़्यादा clear और कम error-prone होता।- दूसरी languages से आदतन parentheses के साथ
while (condition)लिखना -- Rust में अनावश्यक।
whileहर iteration से पहले अपना condition जांचता है और false होते ही रुक जाता है।- Loop body को जो भी condition पर निर्भर करता है उसे update करना चाहिए, नहीं तो loop कभी खत्म नहीं होता।
whileउस तरह एक value produce नहीं करता जैसेloopbreak valueसे कर सकता है।- किसी known range या collection पर iterate करते समय
whileसे ज़्यादाforपसंद करें -- यह ज़्यादा सुरक्षित और idiomatic है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: