scala自身是没有continue,break这两个语法关键词的。

但是实际上我们还是很希望有这两个语法,那么我们是否可以自己实现呢?

  • 从官网上搜索,我们可以找到一下关于break的类相关资料:

Breaks extends AnyRef

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.

  • continue测试:
import util.control.Breaks._

/**
* Created by Administrator on 2016/11/15.
*/
object MyMain {
def main(args: Array[String]): Unit = {
println("Hello World") for (index <- 1 to 10) {
breakable {
if (index == 6) {
println("the index is :"+index)
break()
}
else {
println("hello" + index)
}
}
}
}
}

输出结果
Hello World
hello1
hello2
hello3
hello4
hello5
the index is :6
hello7
hello8
hello9
hello10

Process finished with exit code 0

  • break测试:
import util.control.Breaks._

/**
* Created by Administrator on 2016/11/15.
*/
object MyMain {
def main(args: Array[String]): Unit = {
println("Hello World")
breakable {
for (index <- 1 to 10) {
if (index == 6) {
println("the index is :" + index)
break()
}
else {
println("hello" + index)
}
}
}
}
}

或者

import util.control.Breaks._

/**
* Created by Administrator on 2016/11/15.
*/
object MyMain {
def main(args: Array[String]): Unit = {
println("Hello World")
for (index <- 1 to 10) {
if (index == 6) {
println("the index is :" + index)
break
}
else {
println("hello" + index)
}
}
}
}

输出结果

Hello World
hello1
hello2
hello3
hello4
hello5
the index is :6

参考资料:scala break & continue http://www.cnblogs.com/rollenholt/p/4119105.html

Scala:没有continue,break怎么办?的更多相关文章

  1. Scala 封装可break和continue的foreach循环

    发现scala里没有break和continue, 有时候实在是需要的话就要自己try catch异常,代码看起来有点蛋疼, 所以封装了一个可break和continue的foreach. impor ...

  2. js补充小知识点(continue,break,ruturn)

    1.continue,break,ruturn eg:1-100的和 $(function () { $("#hello").click(function () { var iNu ...

  3. dead loop、continue & break、while...else语句

    Dead loop 死循环,一经触发就会永远运行下去. continue & break 如果在循环过程中,因为某些原因,你不想继续循环了,就要用到break 或 continue语句. br ...

  4. java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系

    本文关键词: java continue break 关键字 详解 区别  用法 标记  标签 使用 示例 联系   跳出循环 带标签的continue和break 嵌套循环  深入continue ...

  5. day10 while else continue break

    a. while else b. continue   break                   continue ,终止当前循环,开始下一次循环                   break ...

  6. 4.4 Go goto continue break

    4.4 Go goto continue break Go语言的goto语句可以无条件的跳转到指定的代码行执行. goto语句一般与条件语句结合,实现条件转义,跳出循环体等. Go程序不推荐使用got ...

  7. Scala中实现break与continue

    Scala是函数式编程语言,因此没有直接的break与continue关键字,要实现break与continue效果,需要绕一下. 需要导入包: import util.control.Breaks. ...

  8. continue break 区别

    在循环中有两种循环方式 continue , break continue 只是跳出本次循环, 不在继续往下走, 还是开始下一次循环 break  将会跳出整个循环, 此循环将会被终止 count = ...

  9. js 之 continue break return 用法及注意事项

    1,continue continue有两种用法: 1,continue; 这种用法必须包含在循环里,否则报错,例子: for(var i=0;i<10;i++){ if(i%2===0){ c ...

  10. Java goto,continue,break,标签

    goto:在Java中goto仍是保留字,但并未在语言中使用它:Java没有goto. 保留字的定义:       保留字(reserved word),指在高级语言中已经定义过的字,使用者不能再将这 ...

随机推荐

  1. Codeforces Round #365 (Div. 2)

    A题 Mishka and Game 水..随便统计一下就A了 #include <cstdio> #include <map> #include <set> #i ...

  2. B窗体继承于A窗体,B启动:问题点

    uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls; t ...

  3. 【原】iOS学习之极光推送

    一.极光推送工程端 1.下载SDK 极光推送是一个推送消息的第三方,SDK下载:https://www.jpush.cn/common/products 集成压缩包内容:包名为JPush-iOS-SD ...

  4. ZeroMQ接口函数之 :zmq_disconnect - 断开一个socket的连接

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_disconnect zmq_disconnect(3) ØMQ Manual - ØMQ/3.2.5 Name ...

  5. SQLServer注入技巧

    一.对于SA权限的用户执行命令,如何获取更快捷的获取结果? 有显示位 无显示位 其实这里的关键并不是有无显示位.exec master..xp_cmdshell 'systeminfo'生成的数据写进 ...

  6. win7下装完ubuntu linux后,开机画面怎直接进入linux了,win7怎么启动

    修复 Windows 7 启动项重新启动ubuntu之后,结果发现选择windows 7的启动项,又进入到Ubuntu的安装界面.下面来说明如何修复,进人Ubuntu系统,打开“应用程序---附件-- ...

  7. Java Service Wrapper简介与使用

    在实际开发过程中很多模块需要独立运行,他们并不会以web形式发布,传统的做法是将其压缩为jar包独立运行,这种形式简单易行也比较利于维护,但是一旦服务器重启或出现异常时,程序往往无法自行修复或重启.解 ...

  8. package.json

    1,项目按住shift,右击鼠标:"在此处打开命令行窗口" 2,cmd输入:npm init 输入name,varsion....license项的信息,yes 3,此项目中自动创 ...

  9. Redmi Note3 hennessy 刷机过程记录

    本文只是凭记忆,记录大致的步骤,提供线索. 准备 刷机包和supersu刷机包,到xiaomi.eu上下载, 如果是稳定版可能有锁bootloader,需要到下载解锁软件.开发版无锁 刷入recove ...

  10. javascript 杂记

    博客 http://www.cnblogs.com/onepixel/ http://www.cnblogs.com/ahthw/p/4240220.html#javascript call.appl ...