Scala while and do/while loop

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 04:52:28 Viewed : 512


Scala while and do/while loop

In Scala, you can use both while and do/while loops to create iterative control flow in your programs. These loops work similarly to loops in other programming languages.

  1. While Loop: The while loop repeatedly executes a block of code as long as a given condition is true. Here is the syntax:

    scala
    while (condition) { // Code to be executed while the condition is true }

    Example of a while loop:

    scala
    var i = 0 while (i < 5) { println(i) i += 1 }

    This code will print numbers from 0 to 4 because the loop continues executing as long as i is less than 5.

  2. Do/While Loop: The do/while loop is similar to the while loop, but it guarantees that the code block is executed at least once before checking the condition for continuation. Here is the syntax:

    scala
    do { // Code to be executed at least once } while (condition)

    Example of a do/while loop:

    scala
    var i = 0 do { println(i) i += 1 } while (i < 5)

    This code will also print numbers from 0 to 4. Even if i starts out greater than or equal to 5, the loop will execute at least once because the condition is checked after the code block.

It is important to be cautious when using while and do/while loops to avoid infinite loops. Make sure the loop condition eventually becomes false, or you include a mechanism to break out of the loop when needed.

Here are examples of both while and do/while loops in Scala with explanations and their respective outputs:

While Loop:

scala
var i = 0 while (i < 5) { println(s"While loop: i = $i") i += 1 }

In this example, the while loop will execute as long as i is less than 5. It prints the value of i in each iteration.

Output:

vbnet
While loop: i = 0 While loop: i = 1 While loop: i = 2 While loop: i = 3 While loop: i = 4

The loop stops when i reaches 5 because i < 5 is no longer true.

Do/While Loop:

scala
var j = 0 do { println(s"Do/While loop: j = $j") j += 1 } while (j < 5)

In this example, the do/while loop will execute the code block at least once, and then it checks if j is less than 5 to determine if it should continue.

Output:

vbnet
Do/While loop: j = 0 Do/While loop: j = 1 Do/While loop: j = 2 Do/While loop: j = 3 Do/While loop: j = 4

The loop behaves similarly to the while loop but guarantees that the code block runs at least once before checking the condition.

Both loops in these examples stop when the loop control variable (i or j) becomes equal to or greater than 5, as specified by the loop conditions.

While and do/while loops and expressions syntax as follows

     ·        while loop

           while (condition) {

              statement(a)

              statement(b)

            } 

·        do-while

  do {

    statement(a)

    statement(b)

  } while (condition)

 Example:

Following example illustrates about Scala while and do/while loop

Save the file as −  WhileDoWhile.scala

 WhileDoWhile.scala  //File nam

package runnerdev

object WhileDoWhile {

  def main(args: Array[String]) { 

    var x = 10; // Initialization

    while (x <= 20) { // Condition

      println("x value is " + x);

      x = x * 2 // Increment

    } 

    var y = 1; // Initialization

    do {

      println("y value is " + y);

      y = y + 2; // Increment

    } while (y <= 5) // Condition 

  } 

}

compile and run the above example as follows 

scala> scalac WhileDoWhile.scala

scala> scala WhileDoWhile 

OutPut:

x value is 10

x value is 20

y value is 1

y value is 3

y value is 5

Search
Related Articles

Leave a Comment: