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

Nested Loops क्या हैं

कभी-कभी आपको एक loop के अंदर एक और loop चाहिए होता है, जैसे एक grid के हर square को एक समय में एक row और column जांचना।
Syntax
rust
'label: for outer in outer_iter {
    for inner in inner_iter {
        if condition {
            break 'label;
        }
    }
}

A Basic Nested Loop

एक for loop को दूसरे के अंदर रखना आपको दो ranges के हर combination पर iterate करने देता है, जो grids, tables, और matrices process करने का आधार है।

उदाहरण: A Basic Nested Loop

markup
// Entry point: execution of the program starts here
fn main() {
    // Loop over `1..=3`, binding each item to `row`
    for row in 1..=3 {
        // Loop over `1..=3`, binding each item to `col`
        for col in 1..=3 {
            // Print "({},{}) " to the console, substituting in `row, col` (without a newline)
            print!("({},{}) ", row, col);
        }
        // Print a blank line to the console (with a trailing newline)
        println!();
    }
}

Breaking the Inner Loop Only

Default रूप से, break सिर्फ उस innermost loop से बाहर निकलता है जिसमें यह लिखा गया है। Outer loop अपनी बाकी iterations सामान्य रूप से चलाता रहता है।

उदाहरण: Breaking the Inner Loop Only

markup
// Entry point: execution of the program starts here
fn main() {
    // Loop over `1..=3`, binding each item to `i`
    for i in 1..=3 {
        // Loop over `1..=5`, binding each item to `j`
        for j in 1..=5 {
            // Check whether `j == 2`
            if j == 2 {
                break;
            }
            // Print "i={}, j={}" to the console, substituting in `i, j` (with a trailing newline)
            println!("i={}, j={}", i, j);
        }
    }
}

Breaking the Outer Loop with a Label

किसी nested inner loop के अंदर से एक outer loop से बाहर निकलने के लिए, outer loop को label करें और उस label को break के साथ उपयोग करें। यह precise control देता है कि कौन सा loop level असल में रुकता है।

उदाहरण: Breaking the Outer Loop with a Label

markup
// Entry point: execution of the program starts here
fn main() {
    'search: for i in 1..=3 {
        // Loop over `1..=3`, binding each item to `j`
        for j in 1..=3 {
            // Check whether `i == 2 && j == 2`
            if i == 2 && j == 2 {
                // Print "Found match at i={}, j={}" to the console, substituting in `i, j` (with a trailing newline)
                println!("Found match at i={}, j={}", i, j);
                break 'search;
            }
        }
    }
    // Print "Search complete" to the console (with a trailing newline)
    println!("Search complete");
}

Building a Multiplication Table

Nested loops आमतौर पर computed values की एक पूरी table बनाने के लिए उपयोग होते हैं, outer loop पर rows और inner loop पर columns iterate करते हुए।

उदाहरण: Building a Multiplication Table

markup
// Entry point: execution of the program starts here
fn main() {
    // Loop over `1..=3`, binding each item to `i`
    for i in 1..=3 {
        // Loop over `1..=3`, binding each item to `j`
        for j in 1..=3 {
            // Print "{}\t" to the console, substituting in `i * j` (without a newline)
            print!("{}\t", i * j);
        }
        // Print a blank line to the console (with a trailing newline)
        println!();
    }
}
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. किसी nested loop के अंदर unlabeled break उपयोग करना जब आपका मतलब सिर्फ inner loop के बजाय outer loop से बाहर निकलना था।
  2. ऐसी problem के लिए गहराई से nested loops लिखना जो .flat_map() या .zip() जैसे iterator combinators से बेहतर solve हो सके।
  3. यह भूल जाना कि outer loop body के अंदर लेकिन inner loop के बाहर declare की गई variables हर outer iteration पर reset होती हैं।
चैप्टर सारांश
  • किसी दूसरे loop की body के अंदर लिखा गया loop एक nested loop है, grid-जैसी या combinatorial problems के लिए उपयोगी।
  • 'outer: जैसे labels break/continue को innermost के बजाय एक specific loop level target करने देते हैं।
  • Nested loops iteration counts को multiply करते हैं, इसलिए एक 10x10 nested loop inner body को 100 बार चलाता है।
  • साधारण transformations के लिए, iterator methods अक्सर nested loops को clearer, equally fast code से replace कर सकते हैं।
🔒

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.