loop Keyword का उपयोग
loop आपके program को किसी चीज़ को हमेशा के लिए दोहराने देता है, जब तक आप इसे रुकने को न कहें।loop {
// body
if condition {
break;
}
}
A Basic Infinite Loop
loop keyword अपने block को हमेशा के लिए दोहराता है जब तक एक break statement explicitly इससे बाहर न निकले। यह तब उपयोगी है जब आपको पहले से पता न हो कि कितनी iterations चाहिए।
उदाहरण: A Basic Infinite Loop
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `count`, initialized to `0`
let mut count = 0;
// Loop forever until an explicit `break`
loop {
count += 1;
// Print "count = {}" to the console, substituting in `count` (with a trailing newline)
println!("count = {}", count);
// Check whether `count == 3`
if count == 3 {
break;
}
}
}
Login to try C/C++/Java/PHP code in the editor
Returning a Value from loop
ज़्यादातर looping constructs के विपरीत, loop एक expression है: आप break को एक value पास कर सकते हैं, और वह पूरे loop expression की value बन जाती है जिसे यह evaluate करता है।
उदाहरण: Returning a Value from loop
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `counter`, initialized to `0`
let mut counter = 0;
// Declare an immutable variable `result`; its value comes from the block below
let result = loop {
counter += 1;
// Check whether `counter == 5`
if counter == 5 {
break counter * 2;
}
};
// Print "result = {}" to the console, substituting in `result` (with a trailing newline)
println!("result = {}", result);
}
Login to try C/C++/Java/PHP code in the editor
Labeled Loops
जब loops nested होते हैं, आप outer वाले को 'outer: जैसे नाम से label कर सकते हैं और उस label को break या continue के साथ उपयोग करके सीधे inner वाले के अंदर से outer loop control कर सकते हैं।
उदाहरण: Labeled Loops
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `total`, initialized to `0`
let mut total = 0;
'outer: loop {
// Declare a mutable variable `inner_count`, initialized to `0`
let mut inner_count = 0;
// Loop forever until an explicit `break`
loop {
total += 1;
inner_count += 1;
// Check whether `inner_count == 2`
if inner_count == 2 {
break;
}
// Check whether `total >= 6`
if total >= 6 {
break 'outer;
}
}
// Check whether `total >= 6`
if total >= 6 {
break;
}
}
// Print "total = {}" to the console, substituting in `total` (with a trailing newline)
println!("total = {}", total);
}
Login to try C/C++/Java/PHP code in the editor
loop for Retry Logic
loop का एक आम real-world उपयोग किसी operation को तब तक retry करना है जब तक यह succeed न हो, सिर्फ top पर नहीं बल्कि body के अंदर कहीं एक condition जांचते हुए।
उदाहरण: loop for Retry Logic
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `attempts`, initialized to `0`
let mut attempts = 0;
// Declare an immutable variable `success`; its value comes from the block below
let success = loop {
attempts += 1;
// Check whether `attempts == 3`
if attempts == 3 {
break true;
}
};
// Print "Succeeded after {} attempts: {}" to the console, substituting in `attempts, success` (with a trailing newline)
println!("Succeeded after {} attempts: {}", attempts, success);
}
Login to try C/C++/Java/PHP code in the editor
- एक infinite
loopलिखना और एकbreakcondition भूल जाना, program को हमेशा के लिए hang करने का कारण बनते हुए। - यह न समझना कि
loopbreak value;के ज़रिए एक value return कर सकता है, और इसके बजाय manually एक result variable track करना। loop(हमेशा दोहराता है) कोwhile(सिर्फ एक condition true रहने तक दोहराता है) के साथ confuse करना।
loopएक code block को indefinitely दोहराता है जब तक एकbreakstatement reach न हो।break value;एकloopसे बाहर निकल सकता है साथ ही एक value भी produce कर सकता है जिस तकloopexpression evaluate होता है।- Loop labels (
'outer: loop { ... })break/continueको एक specific outer loop target करने देते हैं। loopअक्सर retry logic के लिए या जब exit condition body के बीच में जांचा जाता है तब उपयोग होता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: