← Back to Swift Course | Chapter 3: Control Flow | Lesson 6 of 8

break और continue

break एक loop को पूरी तरह रोकता है, जबकि continue बस current lap का बाकी हिस्सा skip करता है और अगले पर चला जाता है।
Syntax
swift
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

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

Using continue to Skip an Iteration

continue current iteration के बचे code को skip करता है और सीधे अगली पर jump करता है, loop को पूरी तरह रोके बिना।

उदाहरण: Using continue to Skip an Iteration

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

Labeled Loops

एक loop से पहले रखा एक label break या continue को उस specific loop को target करने देता है यहां तक कि एक nested loop के अंदर से भी।

उदाहरण: Labeled Loops

markup
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)")
    }
}
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. break उपयोग करना जब मकसद सिर्फ current iteration skip करना था; उसके लिए continue चाहिए, break नहीं।
  2. यह भूल जाना कि एक switch के अंदर break switch को खुद exit करता है, किसी enclosing loop को नहीं -- उस case के लिए एक labeled break उपयोग करें।
  3. एक continue के बाद उसी iteration में code रखना यह उम्मीद से कि यह अब भी चलेगा; continue के बाद उस pass के लिए कुछ भी execute नहीं होता।
चैप्टर सारांश
  • break तुरंत nearest enclosing loop (या switch) से exit करता है।
  • continue current iteration का बाकी हिस्सा skip करता है और अगली पर जाता है।
  • Labeled loops (outerLoop: for ...) break या continue को एक specific outer loop target करने देते हैं।
  • दोनों आमतौर पर loop flow control करने के लिए एक if condition के साथ combined होते हैं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.