Scala do...while Loop
Unlike the while loop, which tests its condition at the head of the loop, in Scala the do...while loop checks its condition at the end of the loop.
The do...while loop is similar to the while loop, but the do...while loop ensures that the loop is executed at least once.
Syntax
In the Scala language, while The syntax of the do...while loop:
do {
statement(s);
} while( condition );
Flowchart
Note that the condition expression appears at the bottom of the loop, so the statement(s) in the loop execute at least once before the condition is tested.
If the condition is true, control flow jumps back to the do above and re-executes the statement(s) in the loop.
This process repeats continuously until the given condition becomes false.
Example
Example
object
Test
{
def
main
(
args
:
Array
[
String
]
)
{
// Local variable
var
a
=
10
;
// do loop
do
{
println
(
"Value of a: "
- a
)
;
a
=
a +
1
;
}
while
(
a
<
20
)
}
}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