107
Scala while loop
In Scala, while loop is used to iterate code till the specified condition. It tests boolean expression and iterates again and again. You are recommended to use while loop if you don’t know number of iterations prior.
Syntax
Flowchart:
Scala while loop Example
Output:
10 12 14 16 18 20
Scala Infinite While Loop Example
You can also create an infinite while loop. In the below program, we just passed true in while loop. Be careful, while using infinite loop.
Output:
10 12 14 16 ... Ctr+Z// To stop execution
Scala do-while loop example
Output:
10 12 14 16 18 20
Scala Infinite do-while loop
In scala, you can create infinite do-while loop. To create infinite loop just pass true literal in loop condition.
Let’s see an example.
Output:
10 12 14 16 ... Ctrl+Z// To stop execution of program
Next TopicScala for loop