Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 10:22:28 Viewed : 218
Scala while and do/while loop
While and do/while loops and expressions syntax as follows
while
(condition) {
statement(a)
statement(b)
}
·
do-while
do {
statement(a)
statement(b)
} while (condition)
Following example illustrates about Scala while and do/while
loop
Save the file as − WhileDoWhile.scala
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