Scala 循环(while, do…while, for, 嵌套循环)
在编程语言中,循环是一种功能,它有助于在某些条件评估为真时重复执行一组指令/函数。循环使程序员的任务更加简单。Scala提供了不同类型的循环来处理程序中基于条件的情况。Scala中的循环有:
- while 循环
- do…while 循环
- for 循环
- 嵌套循环
while 循环
while 循环通常在括号中输入一个条件。如果该条件为 “真 “,则执行while循环主体中的代码。当我们不知道希望循环执行的次数,但知道循环的终止条件时,就可以使用while循环。它也被称为 入口控制循环,因为在执行循环之前要检查条件。while循环可以被认为是一个重复的if语句。
语法:
while (condition)
{
// Code to be executed
}
流程图:

- While循环从检查条件开始。如果评价为真,则执行循环主体语句,否则执行循环后的第一个语句。由于这个原因,它也被称为入口控制循环。
- 一旦条件被评估为真,循环体中的语句将被执行。通常情况下,这些语句包含了为下一次迭代处理的变量的更新值。
- 当条件变为假时,循环终止,标志着其生命周期的结束。
例如:
// Scala program to illustrate while loop
object whileLoopDemo
{
// Main method
def main(args: Array[String])
{
var x = 1;
// Exit when x becomes greater than 4
while (x <= 4)
{
println("Value of x: " + x);
// Increment the value of x for
// next iteration
x = x + 1;
}
}
}
输出:
Value of x: 1
Value of x: 2
Value of x: 3
Value of x: 4
无限次的while循环: while循环可以无限次地执行,这意味着这个循环没有终止条件。换句话说,我们可以说有一些条件始终为真,从而导致while循环无限次执行,或者我们可以说它永远不会终止。
例子: 下面的程序将无限次地打印指定的语句,同时在在线IDE上给出运行时错误 Killed(SIGKILL) 。
// Scala program to illustrate Infinite while loop
object infinitewhileLoopDemo
{
// Main method
def main(args: Array[String])
{
var x = 1;
// this loop will never terminate
while (x < 5)
{
println("GeeksforGeeks")
}
}
}
输出:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
.
.
.
.
do…while 循环
do…while循环与while循环几乎相同。唯一的区别是do.while循环至少运行一次。条件在第一次执行后被检查。当我们希望循环至少运行一次时,就可以使用do…while循环。它也被称为 退出控制循环 ,因为条件在执行循环后被检查。
语法:
do {
// statements to be Executed
} while(condition);
流程图:

示例:
// Scala program to illustrate do..while loop
object dowhileLoopDemo
{
// Main method
def main(args: Array[String])
{
var a = 10;
// using do..while loop
do
{
print(a + " ");
a = a - 1;
}while(a > 0);
}
}
输出:
10 9 8 7 6 5 4 3 2 1
for 循环
for循环的功能与while循环类似,但语法不同。当事先知道循环语句的执行次数时,for循环是首选。Scala中的for循环 “有很多变化,我们将在接下来的文章中讨论。基本上,它是一种重复控制结构,允许程序员编写一个需要执行特定次数的循环。
例子:
// Scala program to illustrate for loop
object forloopDemo {
// Main Method
def main(args: Array[String]) {
var y = 0;
// for loop execution with range
for(y <- 1 to 7)
{
println("Value of y is: " + y);
}
}
}
输出:
Value of y is: 1
Value of y is: 2
Value of y is: 3
Value of y is: 4
Value of y is: 5
Value of y is: 6
Value of y is: 7
嵌套循环
循环中包含一个循环,这种循环被称为嵌套循环。它可以在for循环中包含for循环,在while循环中包含while循环。也有可能一个while循环包含for循环,反之亦然。
例子:
// Scala program to illustrate nested loop
object nestedLoopDemo {
// Main Method
def main(args: Array[String]) {
var a = 5;
var b = 0;
// outer while loop
while (a < 7)
{
b = 0;
// inner while loop
while (b < 7 )
{
// printing the values of a and b
println("Value of a = " +a, " b = "+b);
b = b + 1;
}
// new line
println()
// incrementing the value of a
a = a + 1;
// displaying the updated value of a
println("Value of a Become: "+a);
// new line
println()
}
}
}
输出:
(Value of a = 5, b = 0)
(Value of a = 5, b = 1)
(Value of a = 5, b = 2)
(Value of a = 5, b = 3)
(Value of a = 5, b = 4)
(Value of a = 5, b = 5)
(Value of a = 5, b = 6)
Value of a Become: 6
(Value of a = 6, b = 0)
(Value of a = 6, b = 1)
(Value of a = 6, b = 2)
(Value of a = 6, b = 3)
(Value of a = 6, b = 4)
(Value of a = 6, b = 5)
(Value of a = 6, b = 6)
Value of a Become: 7
极客教程