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. Subsonic的使用之基本语法、操作(2)

    查询 SubSonic2.1版本 – 例出3种查询. Product product = new Select().From<Product>() .Where(Product.Produ ...

  2. JS 之Blob 对象类型

    原文 http://blog.csdn.net/oscar999/article/details/36373183 什么是Blob? Blob 是什么? 这里说的是一种Javascript的对象类型. ...

  3. sqoop部署

    下载安装包 sqoop-1.99.3-bin-hadoop200.tar.gz 解压 tar zxvf sqoop-1.99.3-bin-hadoop200.tar.gz 建立sqoop链接 ln - ...

  4. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  5. CSS的Hack技术

    主要是用来解决兼容性的特殊方法: IE都能识别*;标准浏览器(如FF)不能识别*: IE6能识别*,但不能识别 !important, IE7能识别*,也能识别!important,还有# FF不能识 ...

  6. java的poi技术读,写Excel[2003-2007,2010]

    在上一篇blog:java的poi技术读取Excel[2003-2007,2010] 中介绍了关于java中的poi技术读取excel的相关操作 读取excel和MySQL相关: java的poi技术 ...

  7. bzoj刷水

    因为最近的生活太颓废总是不做题而被老师D了一番, 所以今天晚上到bzoj上去刷了几道水题.....   bzoj 4320: ShangHai2006 Homework 题目大意 维护一个支持两个操作 ...

  8. 简明 Git 命令速查表(中文版)

    原文引用地址:https://github.com/flyhigher139/Git-Cheat-Sheet/blob/master/Git%20Cheat%20Sheet-Zh.md在Github上 ...

  9. 团队作业week9 scenario testing

    1.How do you expect different personas to use your software? What’s their need and their goals,  how ...

  10. hibernate 异常:Unexpected Exception caught setting

    异常信息:Unexpected Exception caught setting 'outHeight' on 'class com.srpm.core.project.seismicFortific ...