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
No comments:
Post a Comment