Scala - while Loop



The while loop repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. A while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax

The following is a syntax for while loop.

while(condition){
   statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.

Flow Chart

Scala while loop

Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example of Scala while Loop

Try the following example program to understand loop control statements (while statement) in Scala Programming Language −

object Demo {
   def main(args: Array[String]) {
      // Local variable declaration:
      var a = 10;

      // while loop execution
      while( a < 20 ){
         println( "Value of a: " + a );
         a = a + 1;
      }
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Scala Nested while Loop

A nested while loop is a loop inside another while loop. The inner loop is executed completely every time the outer loop is executed once.

Syntax

The following is a syntax for nested while loop.

while (condition1) {
   while (condition2) {
      statement(s);
   }
   statement(s);
}

Here, condition1 and condition2 are both expressions that evaluate to true or false. The outer loop (condition1) controls how many times the inner loop (condition2) executes. Each time the outer loop iterates, the inner loop runs completely from start to finish. The statement(s) inside the inner loop execute repeatedly as long as condition2 is true. After the inner loop completes, the statement(s) following the inner loop execute. When condition1 becomes false, program control passes to the line immediately following the outer loop.

Example

Try the following example program to understand nested while loop in Scala Programming Language −

object Demo {
  def main(args: Array[String]) = {
    // Local variable declaration:
    var i = 0
    var j = 0
    
    // while loop execution
    while (i < 3) {
      j = 0
      while (j < 3) {
        println("i: " + i + ", j: " + j)
        j = j + 1
      }
      i = i + 1
    }
  }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

i: 0, j: 0
i: 0, j: 1
i: 0, j: 2
i: 1, j: 0
i: 1, j: 1
i: 1, j: 2
i: 2, j: 0
i: 2, j: 1
i: 2, j: 2
Advertisements