break, continue और Labels
In this page:
for (item in collection) {
if (condition) break // leave the loop
if (condition) continue // skip to next
}
label@ for (a in outer) {
for (b in inner) { break@label }
}
Using break
break तुरंत उस loop से बाहर निकल जाता है जिसके अंदर यह है, बाकी बची हर iteration को skip करते हुए, और execution loop के ठीक बाद वाले code से जारी रहता है।
उदाहरण: Using break
// Entry point: execution of the program starts here
fun main() {
// Loop over `1..10`, binding each item to `i`
for (i in 1..10) {
if (i == 5) break
// Print "i = $i" to the console, with a trailing newline
println("i = $i")
}
// Print "Loop exited" to the console, with a trailing newline
println("Loop exited")
}
Login to try C/C++/Java/PHP code in the editor
Using continue
continue current iteration के body का बाकी हिस्सा skip करता है और सीधे अगली iteration के condition check पर jump करता है।
उदाहरण: Using continue
// Entry point: execution of the program starts here
fun main() {
// Loop over `1..5`, binding each item to `i`
for (i in 1..5) {
if (i % 2 == 0) continue
// Print "Odd number: $i" to the console, with a trailing newline
println("Odd number: $i")
}
}
Login to try C/C++/Java/PHP code in the editor
Labeling a Loop
किसी loop से ठीक पहले name@ लिखना इसे एक label देता है, जिसे फिर किसी nested loop के अंदर से break@name या continue@name से target किया जा सकता है।
उदाहरण: Labeling a Loop
// Entry point: execution of the program starts here
fun main() {
outer@ for (i in 1..3) {
// Loop over `1..3`, binding each item to `j`
for (j in 1..3) {
if (j == 2) continue@outer
// Print "i=$i, j=$j" to the console, with a trailing newline
println("i=$i, j=$j")
}
}
}
Login to try C/C++/Java/PHP code in the editor
Breaking an Outer Loop
nested loops के अंदर एक labeled break@name सिर्फ सबसे अंदरूनी loop के बजाय specifically नामित outer loop से पूरी तरह बाहर निकल जाता है।
उदाहरण: Breaking an Outer Loop
// Entry point: execution of the program starts here
fun main() {
search@ for (i in 1..3) {
// Loop over `1..3`, binding each item to `j`
for (j in 1..3) {
if (i == 2 && j == 2) break@search
// Print "Checking i=$i, j=$j" to the console, with a trailing newline
println("Checking i=$i, j=$j")
}
}
// Print "Search finished" to the console, with a trailing newline
println("Search finished")
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि किसी nested loop के अंदर एक साधारण
breakसभी surrounding loops से बाहर निकल जाता है, जबकि यह सिर्फ सबसे अंदरूनी से बाहर निकलता है जब तक कोई label उपयोग न हो। - किसी label को define या reference करते समय
@symbol भूल जाना, जैसेloop@के बजायloopलिखना। - labeled breaks/continues का ज़्यादा उपयोग करना जहां loops को restructure करना या एक function निकालना ज़्यादा साफ़ होता।
breakसबसे पास वाले enclosing loop से तुरंत बाहर निकलता है;continueइसकी अगली iteration पर skip करता है।- एक label किसी loop से पहले
name@के रूप में लिखा जाता है, औरbreak@nameयाcontinue@nameके रूप में reference किया जाता है। - Labeled
break/continueआपको किसी nested loop के अंदर से एक outer loop control करने देते हैं। - बिना किसी label के,
break/continueसिर्फ उस सबसे अंदरूनी loop को प्रभावित करते हैं जिसमें वे लिखे हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: