scala中的break_Scala中的break语句

本是古典 何须时尚 2023-03-05 09:56 77阅读 0赞

scala中的break

Scala中的break语句 (break statement in Scala)

The break statements are used to terminate the execution of the code block at any point in the block. Loops are not different than a code block. In Scala, there is no break keyword, it was removed from Scala after the release of 2.8 Version. From this version, the break keyword was replaced by the break method that is used in Scala in place of break keyword to terminate Scala loops.

break语句用于在代码块中的任何点终止代码块的执行。 循环与代码块没有不同。 在Scala中,没有break关键字 ,它在2.8版本发布后已从Scala中删除。 在此版本中, break关键字已替换为Scala中使用的break方法来代替break关键字来终止Scala循环。

The break method needs to be imported, this can be done by using the following import statement, import scala.util.control.break.

需要导入break方法 ,这可以通过使用以下import语句import scala.util.control.break来完成 。

The break statement can be used to terminate all type of loop statements like, for, while, do-while and nested loops.

break语句可用于终止所有类型的循环语句,例如for,while,do-while和嵌套循环。

First to import break keyword: import scala.util.control.Breaks._

首先导入break关键字: import scala.util.control.Breaks._

This statement imports the breaks class that contains the break method.

该语句导入包含break方法的breaks类。

Syntax to use:

使用的语法:

  1. var loop = new breaks;
  2. loop.breakable{
  3. {
  4. loop(){
  5. //code to be executed
  6. Loop. break;
  7. }
  8. }
  9. }

Explanation:

说明:

First, the loop object is created that is used to use all the methods of the class. The breakable method is used to handle any exceptions that may come while breaking a statement. The loop inside it is executed as it is and when the break method is encountered the loop is terminated and the program goes out of the loop code.

首先,创建用于使用该类的所有方法的循环对象。 breakable方法用于处理破坏语句时可能出现的任何异常。 它内部的循环按原样执行,当遇到break方法时,循环终止,程序退出循环代码。

Example code:

示例代码:

  1. import scala.util.control._
  2. object MyClass {
  3. def main(args: Array[String]) {
  4. var loop = new Breaks;
  5. var i = 0
  6. loop.breakable{
  7. for(i <- 2 until 10){
  8. println(i);
  9. if(i == 5){
  10. loop.break;
  11. }
  12. }
  13. }
  14. }
  15. }

Output

输出量

  1. 2
  2. 3
  3. 4
  4. 5

Code explanation:

代码说明:

First, we have imported the control package that contains breaks class that contains the break method. This break method is used by making an object of the breaks class. the loop is the object of the breaks class that is used to call breakable and break. The breakable is used to handle all the errors that may occur by the break method call. Then the break method is used to terminate the loop statement.

首先,我们导入了包含breaks类的控件包,其中breaks类包含break方法。 通过使breaks类成为对象来使用该break方法。 循环是breaks类的对象,该类用于调用Breakable和Break。 Breakable用于处理break方法调用可能发生的所有错误。 然后使用break方法终止循环语句。

翻译自: https://www.includehelp.com/scala/break-statement-in-scala.aspx

scala中的break

发表评论

表情:
评论列表 (有 0 条评论,77人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Scala里面break

    scala里面没有像java里面的break,也不能直接使用break,但是scala里面使用了另一种方式来使用break的,它是使用的scala.util.control里面