📜  Scala中的while和do while循环

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

Scala中的while和do while循环

编程语言中的循环是一种有助于在某些条件评估为真时重复执行一组指令/函数的功能。循环使程序员的任务更简单。 Scala 提供了不同类型的循环,但在本文中,我们了解 while 和 do-while 循环。

while 循环

在编程时,可能存在我们需要重复的情况,直到满足条件为止。在这些情况下,使用 while 循环。 while 循环通常采用括号中的条件。如果条件为 True,则执行 while 循环主体内的代码。当我们不知道希望循环执行的次数但我们知道循环的终止条件时,使用 while 循环。循环停止的条件称为中断条件。
句法:

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

流程图:

示例:执行 While 循环

Scala
// Scala program of while loop
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        // variable declaration (assigning 5 to a)
        var a = 5
 
        // loop execution
        while (a > 0)
        {
            println("a is : " + a)
            a = a - 1;
        }
    }
}


Scala
// Scala program of while loop
 
// Creating object
object GFG
{
   // Main method
   def main(args: Array[String])
   {
       // variable declaration (assigning 5 to a)
       var a = Array("do_while", "for", "while")
       var index = 0
        
       // loop execution
       while (index < a.length)
       {
           if(a(index) == "while")
               println("index of while is " + index)
           index = index + 1
       }
   }
}


Scala
// Scala program of do-while loop
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        // variable declaration (assigning 5 to a)
        var a = 5;
 
        // loop execution
        do
        {
            println("a is : " + a);
            a = a - 1;
        }
        while (a > 0);
    }
}


Scala
// Scala program for do-while loop
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        // Declaring an array
        var a = Array("hello", "This", "is", "geeksforgeeks", "bye")
        var str = "bye"
        var i = 0
         
        // loop execution
        do
        {
            println("program is saying " + a(i));
            i = i + 1;
        }
        while (a(i) != str);
    }
}


输出:

a is : 5
a is : 4
a is : 3
a is : 2
a is : 1

示例:在数组中查找元素

斯卡拉

// Scala program of while loop
 
// Creating object
object GFG
{
   // Main method
   def main(args: Array[String])
   {
       // variable declaration (assigning 5 to a)
       var a = Array("do_while", "for", "while")
       var index = 0
        
       // loop execution
       while (index < a.length)
       {
           if(a(index) == "while")
               println("index of while is " + index)
           index = index + 1
       }
   }
}

输出:

index of while is 2

注意:要执行以下命令,请使用 Intellij。将此程序保存为 file_name.scala 格式并在 Intellij 中使用 scala 运行它。

做while循环

do..while 循环与 while 循环几乎相同。唯一的区别是 do..while 循环至少运行一次。在第一次执行后检查条件。当我们希望循环至少运行一次时,使用 do..while 循环。它也称为退出控制循环,因为在执行循环后检查条件。在 while 循环中,条件放置在循环顶部而在 do while 循环中,条件放置在末尾,由于条件的这种定位,do while 下的所有语句都至少执行一次。
句法:

do {

// statements to be Executed

} while(condition);

流程图:

示例:执行 do while 循环

斯卡拉

// Scala program of do-while loop
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        // variable declaration (assigning 5 to a)
        var a = 5;
 
        // loop execution
        do
        {
            println("a is : " + a);
            a = a - 1;
        }
        while (a > 0);
    }
}

输出:

a is : 5
a is : 4
a is : 3
a is : 2
a is : 1

示例:运行循环,直到我们在 Array 中遇到一个字符串

斯卡拉

// Scala program for do-while loop
 
// Creating object
object GFG
{
    // Main method
    def main(args: Array[String])
    {
        // Declaring an array
        var a = Array("hello", "This", "is", "geeksforgeeks", "bye")
        var str = "bye"
        var i = 0
         
        // loop execution
        do
        {
            println("program is saying " + a(i));
            i = i + 1;
        }
        while (a(i) != str);
    }
}

输出:

program is saying hello
program is saying This
program is saying is
program is saying geeksforgeeks

在上面的代码中,不会打印再见