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. YUV RGB播放器 打开, 显示RGB数据

    可以查看RGB像素数据 可以通过菜单栏打开像素数据文件,也可以通过拖拽方式打开文件.如果文件名称中包含了“{w}x{h}”这样的字符串(例如“test_320x420.yuv”),系统会自动解析为该像 ...

  2. Python爬虫学习(2): httplib

    httplib模块实现了HTTP和HTTPS的客户端部分,但是一般不直接使用,经常通过urllib来进行HTTP,HTTPS的相关操作. 如果需要查看其源代码可以通过查找命令定位: find / -n ...

  3. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  4. Python中的高级特性

    1.切片.使用“[”和“]”即可,类似Matlab,可以切list,tuple,字符串等. 2.迭代.Python内置的enumerate函数可以把一个list变成索引-元素对. 3.列表生成式.列表 ...

  5. SqlServer禁用启用触发器、外键约束

    --禁用指定名称触发器 ALTER TABLE tbname DISABLE TRIGGER trigname --恢复指定名称触发器 ALTER TABLE tbname ENABLE TRIGGE ...

  6. JS正则表达式将url转成json格式

    var url = location.search.substr(1); param = {}; console.log(url); url.replace(/([^?&]+)=([^?&am ...

  7. 浏览器版本不支持页面示例 supper.html

    关键点在于<html>标签和js.<!--[if lt IE 10]>的配合 使用360浏览器兼容模式下查看:http://runjs.cn/code <!DOCTYPE ...

  8. 关于string.format() 转

    string.format()函数用来生成具有特定格式的字符串,这个函数有两个参数,第一个参数为格式化串:由指示符和控制格式的字符组成.第二个参数是对应格式中每个代号的各种数据. 格式字符串可能包含以 ...

  9. AFNetwork源码解析

    1.关于AFRequestSerializer: 这里分好几个部分,我们首先从NSMutableRequest的相关方法来出发: 比如我们要上传一个文件,那么需要些很麻烦的请求体: HTTP请求头我们 ...

  10. 百度地图api

    引入js <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak ...