How to break out of the loop in scala

When jumping out of the loop in java, we can just break directly, but there is no break in scala, so how to break out of the loop?

Look directly at the demo below:

package test

import scala.util.control.Breaks

object ListDemo {
  def main(args: Array[String]): Unit = {
    var loop = Breaks
    var i = 0
    loop.breakable {
      while (i < 10) {
        println(i)
        i += 1
        if (i == 5) {
          loop.break()
        }
      }
    }
  }
}

This place needs to be included with loop.breakable, otherwise an error will be reported

Exception in thread "main" scala.util.control.BreakControl

Let's take a look at the source code of Breaks:

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala
package util.control

/** A class that can be instantiated for the break control abstraction.
 *  Example usage:
 *  {
   
   {
   
   {
 *  val mybreaks = new Breaks
 *  import mybreaks.{break, breakable}
 *
 *  breakable {
 *    for (...) {
 *      if (...) break()
 *    }
 *  }
 *  }}}
 *  Calls to break from one instantiation of `Breaks` will never
 *  target breakable objects of some other instantiation.
 */
class Breaks {

  private val breakException = new BreakControl

  /**
   * A block from which one can exit with a `break`. The `break` may be
   * executed further down in the call stack provided that it is called on the
   * exact same instance of `Breaks`.
   */
  def breakable(op: => Unit) {
    try {
      op
    } catch {
      case ex: BreakControl =>
        if (ex ne breakException) throw ex
    }
  }

  sealed trait TryBlock[T] {
    def catchBreak(onBreak: =>T): T
  }

  /**
   * This variant enables the execution of a code block in case of a `break()`:
   * {
   
   {
   
   {
   * tryBreakable {
   *   for (...) {
   *     if (...) break()
   *   }
   * } catchBreak {
   *   doCleanup()
   * }
   * }}}
   */
  def tryBreakable[T](op: =>T) = new TryBlock[T] {
    def catchBreak(onBreak: =>T) = try {
      op
    } catch {
      case ex: BreakControl =>
        if (ex ne breakException) throw ex
        onBreak
    }
  }

  /**
   * Break from dynamically closest enclosing breakable block using this exact
   * `Breaks` instance.
   *
   * @note This might be different than the statically closest enclosing block!
   */
  def break(): Nothing = { throw breakException }
}

/** An object that can be used for the break control abstraction.
 *  Example usage:
 *  {
   
   {
   
   {
 *  import Breaks.{break, breakable}
 *
 *  breakable {
 *    for (...) {
 *      if (...) break
 *    }
 *  }
 *  }}}
 */
object Breaks extends Breaks

private class BreakControl extends ControlThrowable

As can be seen from the above source code, the principle of combining breakable and break methods to control the loop is to use the break method to throw an exception, and then the breakable method catches the exception, thereby ending the execution of the code in the entire breakable method block, but does not affect Breakable method in vitro code execution, so as to achieve control.

If there is something wrong, please correct me. If you have any questions, you can add QQ group: 340297350 Thank you

Guess you like

Origin blog.csdn.net/xianpanjia4616/article/details/88320682