break और continue
break एक loop को पूरी तरह रोकता है, जबकि continue बस current lap का बाकी हिस्सा skip करता है और अगले पर चला जाता है।
Syntax
for item in collection {
if condition { break } // leave the loop
if condition { continue } // skip to next item
}
label: for a in outer {
for b in inner { continue label }
}
Using break to Exit Early
break तुरंत loop को पूरी तरह रोक देता है, किसी भी बची iterations को skip करते हुए।
उदाहरण: Using break to Exit Early
// Loop over `1...10`, binding each item to `number`
for number in 1...10 {
// Check whether `number == 4`
if number == 4 {
break
}
// Print "Number: \(number)" to the console
print("Number: \(number)")
}
Login to try C/C++/Java/PHP code in the editor
Using continue to Skip an Iteration
continue current iteration के बचे code को skip करता है और सीधे अगली पर jump करता है, loop को पूरी तरह रोके बिना।
उदाहरण: Using continue to Skip an Iteration
// Loop over `1...6`, binding each item to `number`
for number in 1...6 {
// Check whether `number % 2 == 0`
if number % 2 == 0 {
continue
}
// Print "Odd number: \(number)" to the console
print("Odd number: \(number)")
}
Login to try C/C++/Java/PHP code in the editor
Labeled Loops
एक loop से पहले रखा एक label break या continue को उस specific loop को target करने देता है यहां तक कि एक nested loop के अंदर से भी।
उदाहरण: Labeled Loops
outer: for i in 1...3 {
// Loop over `1...3`, binding each item to `j`
for j in 1...3 {
// Check whether `j == 2`
if j == 2 {
continue outer
}
// Print "i=\(i), j=\(j)" to the console
print("i=\(i), j=\(j)")
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
breakउपयोग करना जब मकसद सिर्फ current iteration skip करना था; उसके लिएcontinueचाहिए,breakनहीं।- यह भूल जाना कि एक
switchके अंदरbreakswitch को खुद exit करता है, किसी enclosing loop को नहीं -- उस case के लिए एक labeled break उपयोग करें। - एक
continueके बाद उसी iteration में code रखना यह उम्मीद से कि यह अब भी चलेगा;continueके बाद उस pass के लिए कुछ भी execute नहीं होता।
चैप्टर सारांश
breakतुरंत nearest enclosing loop (या switch) से exit करता है।continuecurrent iteration का बाकी हिस्सा skip करता है और अगली पर जाता है।- Labeled loops (
outerLoop: for ...)breakयाcontinueको एक specific outer loop target करने देते हैं। - दोनों आमतौर पर loop flow control करने के लिए एक
ifcondition के साथ combined होते हैं।
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: