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

break, continue और Labels

break और continue किसी loop को जल्दी रोकने या आगे skip करने देते हैं, और labels उन commands को नाम से किसी specific outer loop पर target करने देते हैं।
Syntax
kotlin
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

markup
// 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")
}

Using continue

continue current iteration के body का बाकी हिस्सा skip करता है और सीधे अगली iteration के condition check पर jump करता है।

उदाहरण: Using continue

markup
// 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")
    }
}

Labeling a Loop

किसी loop से ठीक पहले name@ लिखना इसे एक label देता है, जिसे फिर किसी nested loop के अंदर से break@name या continue@name से target किया जा सकता है।

उदाहरण: Labeling a Loop

markup
// 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")
        }
    }
}

Breaking an Outer Loop

nested loops के अंदर एक labeled break@name सिर्फ सबसे अंदरूनी loop के बजाय specifically नामित outer loop से पूरी तरह बाहर निकल जाता है।

उदाहरण: Breaking an Outer Loop

markup
// 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")
}
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 के अंदर एक साधारण break सभी surrounding loops से बाहर निकल जाता है, जबकि यह सिर्फ सबसे अंदरूनी से बाहर निकलता है जब तक कोई label उपयोग न हो।
  2. किसी label को define या reference करते समय @ symbol भूल जाना, जैसे loop@ के बजाय loop लिखना।
  3. 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:

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.