Nested Loops क्या हैं
In this page:
'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
// 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!();
}
}
Login to try C/C++/Java/PHP code in the editor
Breaking the Inner Loop Only
Default रूप से, break सिर्फ उस innermost loop से बाहर निकलता है जिसमें यह लिखा गया है। Outer loop अपनी बाकी iterations सामान्य रूप से चलाता रहता है।
उदाहरण: Breaking the Inner Loop Only
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Building a Multiplication Table
Nested loops आमतौर पर computed values की एक पूरी table बनाने के लिए उपयोग होते हैं, outer loop पर rows और inner loop पर columns iterate करते हुए।
उदाहरण: Building a Multiplication Table
// 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!();
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी nested loop के अंदर unlabeled
breakउपयोग करना जब आपका मतलब सिर्फ inner loop के बजाय outer loop से बाहर निकलना था। - ऐसी problem के लिए गहराई से nested loops लिखना जो
.flat_map()या.zip()जैसे iterator combinators से बेहतर solve हो सके। - यह भूल जाना कि outer loop body के अंदर लेकिन inner loop के बाहर declare की गई variables हर outer iteration पर reset होती हैं।
- किसी दूसरे loop की body के अंदर लिखा गया loop एक nested loop है, grid-जैसी या combinatorial problems के लिए उपयोगी।
'outer:जैसे labelsbreak/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: