Scala:没有continue,break怎么办?
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怎么办?的更多相关文章
- Scala 封装可break和continue的foreach循环
发现scala里没有break和continue, 有时候实在是需要的话就要自己try catch异常,代码看起来有点蛋疼, 所以封装了一个可break和continue的foreach. impor ...
- js补充小知识点(continue,break,ruturn)
1.continue,break,ruturn eg:1-100的和 $(function () { $("#hello").click(function () { var iNu ...
- dead loop、continue & break、while...else语句
Dead loop 死循环,一经触发就会永远运行下去. continue & break 如果在循环过程中,因为某些原因,你不想继续循环了,就要用到break 或 continue语句. br ...
- java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系
本文关键词: java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系 跳出循环 带标签的continue和break 嵌套循环 深入continue ...
- day10 while else continue break
a. while else b. continue break continue ,终止当前循环,开始下一次循环 break ...
- 4.4 Go goto continue break
4.4 Go goto continue break Go语言的goto语句可以无条件的跳转到指定的代码行执行. goto语句一般与条件语句结合,实现条件转义,跳出循环体等. Go程序不推荐使用got ...
- Scala中实现break与continue
Scala是函数式编程语言,因此没有直接的break与continue关键字,要实现break与continue效果,需要绕一下. 需要导入包: import util.control.Breaks. ...
- continue break 区别
在循环中有两种循环方式 continue , break continue 只是跳出本次循环, 不在继续往下走, 还是开始下一次循环 break 将会跳出整个循环, 此循环将会被终止 count = ...
- js 之 continue break return 用法及注意事项
1,continue continue有两种用法: 1,continue; 这种用法必须包含在循环里,否则报错,例子: for(var i=0;i<10;i++){ if(i%2===0){ c ...
- Java goto,continue,break,标签
goto:在Java中goto仍是保留字,但并未在语言中使用它:Java没有goto. 保留字的定义: 保留字(reserved word),指在高级语言中已经定义过的字,使用者不能再将这 ...
随机推荐
- 前端SEO技巧
前几天在慕课网上学习了“SEO在网页制作中的应用”,觉得挺好.挺有用的,今天,特此做了一个小小的笔记,也算是对学习过后的一个总结. 一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时, ...
- nodejs review-01
lesson lesson-code 05 Run your first web server 使用curl //指定方法;显示header信息 curl -X GET -i localhost:30 ...
- C#基础:委托
委托是C#中最为常见的内容.与类.枚举.结构.接口一样,委托也是一种类型.类是对象的抽象,而委托则可以看成是函数的抽象.一个委托代表了具有相同参数列表和返回值的所有函数.比如: delegate in ...
- ArrayList数组列表
ArrayList数组列表 Collection接口和List接口的区别 List接口扩充了Collection接口,添加了索引相关的方法. code example Object get(int i ...
- Django分析之导出为PDF文件
最近在公司一直忙着做exe安装包,以及为程序添加新功能,好久没有继续来写关于Django的东西了….难得这个周末清闲,来了解了解Django的一些小功能也是极好的了~ 那今天就来看看在Django的视 ...
- java单例模式的几种写法比较
概念: Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式单例. 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建 ...
- JAVA面试题1
1.在main(String[] args)方法内是否可以调用一个非静态方法? 答案:不能[public static void main(String[] args){}] 2.同一个文件里是否可以 ...
- iOS开发中常见问题集锦
在iOS开发中,会出现各种各样的问题.今天,就把这些常见的问题以及各位大牛的解决方案汇总下,方便以后查阅: 常见错误: 1. linker command failed with exit code ...
- 【Leetcode】Longest Palindromic Substring
问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...
- Javascript实现页面加载完成后自动刷新一遍清除缓存文件
我们有些时候在加载页面时,会出现缓存文件对当前文件的表现效果有干扰,如有些缓存的样式文件会是页面效果发生改变,这时我们希望页面在加载时能自动刷新一遍清楚缓存文件. 但是由于跳转页面肯定会用到BOM部分 ...