scala中的break_Scala中的break语句

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:

使用的语法:

    var loop = new breaks;
    loop.breakable{
	    {
		    loop(){
			    //code to be executed
			    Loop. break;
		    }
	    }
    }

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:

示例代码:

import scala.util.control._
object MyClass {
      def main(args: Array[String]) {
         var loop = new Breaks;
         var i = 0
         loop.breakable{
             for(i <-  2 until 10){
                 println(i);
                 if(i == 5){
                     loop.break; 
                 }
             }
         }
      }
   }

Output

输出量

2
3
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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值