Loop with while

In this lesson, you will be introduced to the while loop and learn how it is different from the other control structures.


When using the word “while” in a sentence, we relate it to the construct of time. In English, “while” can be used as a conjunction, indicating the occurrence of one event while another one is in place. For instance, you can say “I am reading a book while my sister is getting ready”. This sentence is indicating that the event, reading a book, will continue to take place as long as the event sister is getting ready is in place.

Keeping the above concept in mind, a while loop in Scala behaves the same way. One event is a condition and the other event is a body of code to be executed. The body of code will be executed repeatedly again and again as long as the condition is being fulfilled (holds true).

Control Flow

Let’s look at the control flow of a while loop below.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy