← Back to Scala Course | Chapter 4: Control Flow | Lesson 6 of 7

break और continue (scala.util.control.Breaks)

Scala में कोई simple break या continue commands नहीं हैं, इसलिए यह किसी loop से जल्दी निकलने के लिए एक खास helper उधार लेता है। यह घंटी बजने से पहले class छोड़ने के लिए एक खास pass चाहिए होने जैसा है।
Syntax
scala
import scala.util.control.Breaks._

breakable {
  for (item <- collection) {
    if (condition) break
    // body
  }
}

break and continue (scala.util.control.Breaks)

Scala में कोई built-in break या continue keywords नहीं हैं, क्योंकि जल्दी loop exits इसकी functional style के खिलाफ जाते हैं, लेकिन scala.util.control.Breaks एक break() function प्रदान करता है जिसे breakable { ... } block के अंदर जल्दी exit के लिए उपयोग किया जा सकता है।

Scala में continue का कोई सीधा equivalent बिल्कुल नहीं है -- idiomatic code किसी iteration को skip करने की कोशिश करने के बजाय loop body को if guard से restructure करता है। Breaks को एक escape hatch माना जाता है, रोज़मर्रा का idiomatic Scala नहीं।

Warning: Scala में continue का कोई equivalent नहीं है -- खास values के लिए processing skip करने के लिए loop body के अंदर एक if-guard का उपयोग करें।

उदाहरण: break and continue (scala.util.control.Breaks)

markup
import scala.util.control.Breaks._

object Main extends App {
  breakable {
    for (i <- 1 to 10) {
      if (i > 5) break()
      println(s"Value: $i")
    }
  }

  // No continue keyword -- use an if-guard to skip instead
  for (i <- 1 to 6) {
    if (i % 2 != 0) {
      println(s"Odd: $i")
    }
  }
}
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. loop को breakable { ... } में wrap किए बिना break का उपयोग करना, जो exit करने के बजाय एक exception फेंकता है।
  2. continue keyword ढूंढना, जो मौजूद नहीं है; breakable block किसी iteration को skip करने के लिए loop body के अंदर जाता है।
  3. import scala.util.control.Breaks._ भूल जाना, जिससे break() और breakable नहीं मिलते।
🔒

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.