Loops in Scala: A loop is a statement that can execute a block of code multiple times based on some condition.
Scala中的循环:循环是一个语句,可以根据某些条件多次执行代码块。
In Scala, there are three types of loops,
在Scala中,存在三种循环类型,
for loop
for循环
while loop
while循环
do...while loop
做... while循环
打破循环 (break a loop)
To break a loop in Scala, we use the break statements as there is no direct break statement instead, there is a break method that is used to break a loop in Scala.
为了中断Scala中的循环,我们使用break语句,因为没有直接的break语句,而是有一个break方法用于中断Scala中的循环。
The break method needs to be imported using:
需要使用以下方法导入break方法:
import scala.util.control.Breaks._
Syntax:
句法:
The following syntax is used to break a loop,
以下语法用于中断循环,
var loop = new breaks;
Loop.breakable{
loop(){
//loop body
Loop.break;
}
}
1)中断for循环 (1) break a for loop)
The for loop is used to execute a block of code multiple times in a program.
for循环用于在程序中多次执行代码块。
Example:
例:
// Program to break a for loop in Scala
import scala.util.control._
object MyClass {
def main(args: Array[String]) {
var loop = new Breaks;
loop.breakable{
for(i <- 1 to 10){
println(i*5);
// the loop will break at i = 6
if(i == 6){
loop.break;
}
}
}
}
}
Output:
输出:
5
10
15
20
25
30
Description:
描述:
In the above code, we have displayed how to break a for loop using the break method. We have created a breakable block of code using the object of Breaks class, the object named loop can create a block in which the break statement is applied. In the breakable, we have a for loop which runs from 1 to 10 and returns i*5, but we have used the break statement at with condition. This condition will be true when i = 6 and the flow of code breaks out of the loop at the iteration.
在上面的代码中,我们显示了如何使用break方法来中断for循环。 我们使用Breaks类的对象创建了一个易碎的代码块,名为loop的对象可以创建一个在其中应用break语句的块。 在Breakable中,我们有一个for循环,该循环从1到10并返回i * 5 ,但我们在with条件下使用了break语句。 当i = 6且代码流在迭代时跳出循环时,此条件将成立。
2)打破一会儿循环 (2) break a while loop)
The while loop in Scala is used to run a block of code multiple numbers of times. The number of executions is defined by an entry condition.
Scala中的while循环用于多次运行代码块。 执行次数由输入条件定义。
The break method can also be used to break out of a while loop.
break方法也可以用于打破while循环。
Example:
例:
// Program to break a while loop in Scala
import scala.util.control._
object MyClass {
def main(args: Array[String]) {
var loop = new Breaks;
var i = 0;
loop.breakable{
while(i < 50){
println(i)
i+= 5
// the loop will break at i > 30
if(i > 30){
loop.break;
}
}
}
}
}
Output:
输出:
0
5
10
15
20
25
30
3)打破do ... while循环 (3) break a do...while loop)
The do...while loop in Scala is used to run a block of code multiple numbers of time. The number of executions is defined by an exit condition.
Scala中的do ... while循环用于多次运行代码块。 执行次数由退出条件定义。
The break method can also be used to break out of a do...while loop.
break方法也可以用于打破do ... while循环。
Example:
例:
// Program to break a do...while loop in Scala
import scala.util.control._
object MyClass {
def main(args: Array[String]) {
var loop = new Breaks;
var i = 0;
loop.breakable{
do{
println(i)
i+= 5
// the loop will break at i > 30
if(i > 30){
loop.break;
}
}
while(i < 50)
}
}
}
Output:
输出:
0
5
10
15
20
25
30