Scala while Loop
As long as the given condition is true, in the Scala language the while loop statement repeatedly executes the code block in the loop body.
Syntax
In the Scala language, while The syntax of the do...while loop:
while(condition)
{
statement(s);
}
Here,statement(s) can be a single statement or a block of several statements.
condition can be any expression, and any non-zero value is treated as true. When the condition is true, the loop is executed. When the condition is false, the loop is exited and program flow continues with the statement immediately following the loop.
Flowchart
Here,while The key point of the loop is that it may not execute even once. When the condition is false, the loop body is skipped and execution proceeds directly to the statement immediately after the while loop.
Example
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
// Local variable
var
a
=
10
;
// while loop execution
while
(
a
<
20
)
{
println
("Value of a: "
- a
)
;
a
=
a +
1
;
}
}
}
The output of the code above is:
$ scalac Test.scala
$ scala Test
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 Loops