do-while Loop
In this page:
do-while Loop
A do { ... } while (condition) loop checks its condition *after* running the body, guaranteeing the body executes at least once -- unlike while, which may skip the body entirely. This construct is deprecated as of Scala 2.13 in favor of restructuring code with while or recursion, though it still compiles with a deprecation warning. It's useful to recognize in older Scala code even if avoided in new code.
Warning: do-while is deprecated since Scala 2.13 -- prefer a while loop with the condition check moved inside, or use recursion instead.
Example: do-while Loop
object Main extends App {
var count = 0
do {
println(s"Running iteration $count")
count += 1
} while (count < 3)
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: