Scala also has a do-while loop which works exactly like the while loop with one added difference that it executes the block of code before checking the condition. do-while ensures that the code in the curly brackets {} will execute at least once.
Syntax
The syntax is as follows.
Let’s look at a very simple example to understand the subtle difference between while and do-while.
The code below declares an immutable variable alwaysOne which is assigned a value of 1. We want to print the value of alwaysOne when it is not 1 (what a paradox!)
As the condition of a != 1 can never be true, the value of a should never be printed. But that’s not the case. Let’s run the same code using while and do-while and see what happens.