Learn How To Use While And Do While Loop

By Nadim Bahadoor | Last updated: June 5, 2018 at 10:00 am

Overview

In this tutorial, we will show how to iterate over some data points using the while loop construct. This is a continuation of our discussion from the previous tutorials on For Comprehension and Ranges.

 

If you are coming from a Java or .NET background, the while construct should be familiar. Just a reminder that from a pure functional perspective the use of loop such as the while loop is generally less favoured.

 

Instead, fold and recursive operations are preferred and if you do not know what these mean, that's OK! We will cover them in upcoming tutorials.

Steps

1. How to use while loop in Scala

Let's declare a mutable variable called numberOfDonutsToBake to represent the quantity of donuts that we need to bake :) We can then use a while loop and keep on decrementing the numberOfDonutsToBake variable by 1 after each iteration.


println("Step 1: How to use while loop in Scala")
var numberOfDonutsToBake = 10
while (numberOfDonutsToBake > 0) {
  println(s"Remaining donuts to be baked = $numberOfDonutsToBake")
  numberOfDonutsToBake -= 1
}

You should see the following output when you run your Scala application in IntelliJ:


Remaining donuts to be baked = 10
Remaining donuts to be baked = 9
Remaining donuts to be baked = 8
Remaining donuts to be baked = 7
Remaining donuts to be baked = 6
Remaining donuts to be baked = 5
Remaining donuts to be baked = 4
Remaining donuts to be baked = 3
Remaining donuts to be baked = 2
Remaining donuts to be baked = 1

NOTE:

  • The while loop will stop when the condition numberOfDonutsToBake > 0 becomes false.
  • Yes - we've used a mutable variable! And we know that mutation is a bad thing in functional programming!
  • Once again, there are better functional ways of achieving the same looping semantics using fold or recursive techniques.

2. How to use do while loop in Scala

If you have used another mainstream language like Java or .NET, I'm pretty sure that you would be familiar with do while.

The difference between a while construct from Step 1 versus a do while is that any expressions within the do {} will be ran at least once regardless of the condition within the while() clause.


println("\nStep 2: How to use do while loop in Scala")
var numberOfDonutsBaked = 0
do {
  numberOfDonutsBaked += 1
  println(s"Number of donuts baked = $numberOfDonutsBaked")
} while (numberOfDonutsBaked < 5)

 

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to use do while loop in Scala
Number of donuts baked = 1
Number of donuts baked = 2
Number of donuts baked = 3
Number of donuts baked = 4
Number of donuts baked = 5

This concludes our tutorial on Learn How To Use While And Do While Loop and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to use while loop in Scala
  • How to use the do while loop in Scala

Tip

  • If you are new to functional programming, you can read more on the better alternatives to using loop such as fold and tail recursion.
  • If fold and tail recursion do not make any sense at the moment - it's perfectly fine! We will go over them in upcoming tutorials.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will go over a more functional side of Scala by showing you its pattern matching features.

 

Stay tuned!

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: