📜  斯卡拉 |循环(while、do..while、for、嵌套循环)

📅  最后修改于: 2022-05-13 01:55:33.343000             🧑  作者: Mango

斯卡拉 |循环(while、do..while、for、嵌套循环)

编程语言中的循环是一种有助于在某些条件评估为真时重复执行一组指令/函数的功能。循环使程序员的任务更简单。 Scala 提供了不同类型的循环来处理程序中基于条件的情况。 Scala 中的循环是:

  • while 循环
  • 做..while循环
  • 循环
  • 嵌套循环

while 循环

while 循环通常采用括号中的条件。如果条件为True ,则执行 while 循环主体内的代码。当我们不知道希望循环执行的次数但我们知道循环的终止条件时,使用 while 循环。它也称为入口控制循环,因为在执行循环之前会检查条件。 while 循环可以被认为是一个重复的 if 语句。
句法:

while (condition)
{
    // Code to be executed
}

流程图:

  • While 循环从检查条件开始。如果它评估为真,则执行循环体语句,否则执行循环后面的第一条语句。出于这个原因,它也被称为入口控制循环。
  • 一旦条件被评估为真,循环体中的语句就会被执行。通常,这些语句包含正在为下一次迭代处理的变量的更新值。
  • 当条件变为假时,循环终止,这标志着其生命周期的结束。

例子:

Scala
// 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;
        }
    }
}


Scala
// 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")
        }
    }
}


Scala
// 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);
    }
}


Scala
// 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);
      }
   }
}


Scala
// 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 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
.
.
.
.

做..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循环具有与 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