while Loop

The Scala while loop isn’t used much. It’s not used in FP at all, and it’s rarely used in OOP, so I’ll only cover it briefly.

As with other languages, it’s used along with var (mutable) fields. However, unlike other languages, we don’t really use method like hasNext and next.

Basic syntax:

while condition do
    statement(a)
    statement(b)

Example:

var x = 1
while x < 5 do
    println(x)
    x += 1

Can optionally add end at the end:

var x = 1
while x < 5 do
    println(x)
    x += 1
end while