Monday, 31 August 2020

Scala: do-while loop

Unlike ‘for’ loop, while and do-while loop are not expressions, these are pure statements. You can convert a ‘for’ from statement to expression using ‘yield’ keyword, but while, do-while loops are pure statements.

 

Syntax

do{

     ......

     ......

} while(condition)

 

Example

Example: Print even numbers till 10 (exclusive)

 

var i = 2; do {

     println(s"i = $i")

     i += 2

}while(i < 10)

scala> var i = 2; do {
     |   println(s"i = $i")
     |  i += 2
     | }while(i < 10)
i = 2
i = 4
i = 6
i = 8
var i: Int = 10



Previous                                                    Next                                                    Home

No comments:

Post a Comment