Work in Progress

We are still in the process of writing the documentation for Scala 3. You can help us to improve the documentation.

Are you searching for the Scala 2 documentation?

Scala 3 Language Reference

Dropped: Do-While

Language

The syntax construct

do <body> while <cond>

is no longer supported. Instead, it is recommended to use the equivalent while loop below:

while ({ <body> ; <cond> }) ()

For instance, instead of

do
  i += 1
while (f(i) == 0)

one writes

while
  i += 1
  f(i) == 0
do ()

The idea to use a block as the condition of a while also gives a solution to the “loop-and-a-half” problem. Here is another example:

while
  val x: Int = iterator.next
  x >= 0
do print(".")

Why Drop The Construct?

  • do-while is used relatively rarely and it can be expressed faithfully using just while. So there seems to be little point in having it as a separate syntax construct.
  • Under the new syntax rules do is used as a statement continuation, which would clash with its meaning as a statement introduction.

Contributors to this page: