← Back to Rust Course | Chapter 3: Control Flow | Lesson 3 of 7

while Loop का उपयोग

while एक code block को सिर्फ तब तक दोहराता है जब तक एक condition true रहे।
Syntax
rust
while condition {
    // body
}

Basic while Loop

एक while loop हर iteration से पहले अपना boolean condition जांचता है और उस condition के false होते ही रुक जाता है। जब iterations की संख्या पहले से पता न हो तो यह आदर्श है।

उदाहरण: Basic while Loop

markup
// 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!");
}

while with a Complex Condition

एक while loop में condition && और || जैसे logical operators उपयोग करके कई checks combine कर सकता है, loop को कई बदलते state pieces पर respond करने देते हुए।

उदाहरण: while with a Complex Condition

markup
// 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);
}

Accumulating with while

while loops आमतौर पर iterations में एक result accumulate करने के लिए उपयोग होते हैं, जैसे values का sum लेना, जब तक कोई stopping condition reach न हो।

उदाहरण: Accumulating with while

markup
// 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);
}

Avoiding Infinite Loops

चूंकि while यह जानने के लिए पूरी तरह अपने condition पर निर्भर है कि कब रुकना है, यह सुनिश्चित करना आपकी ज़िम्मेदारी है कि loop body अंततः उस condition को false बना दे। यह भूल जाने से एक program बनता है जो हमेशा के लिए चलता है।

उदाहरण: Avoiding Infinite Loops

markup
// 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!");
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Loop की condition variable को body के अंदर update करना भूल जाना, एक infinite loop का कारण बनते हुए।
  2. while उपयोग करना जहां एक range या iterator पर एक for loop ज़्यादा clear और कम error-prone होता।
  3. दूसरी languages से आदतन parentheses के साथ while (condition) लिखना -- Rust में अनावश्यक।
चैप्टर सारांश
  • while हर iteration से पहले अपना condition जांचता है और false होते ही रुक जाता है।
  • Loop body को जो भी condition पर निर्भर करता है उसे update करना चाहिए, नहीं तो loop कभी खत्म नहीं होता।
  • while उस तरह एक value produce नहीं करता जैसे loop break value से कर सकता है।
  • किसी known range या collection पर iterate करते समय while से ज़्यादा for पसंद करें -- यह ज़्यादा सुरक्षित और idiomatic है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.