Get startedGet started for free

While loops

1. While loops

Well done on getting through the first lesson on loops! In this lesson we are going to extend your knowledge of loops further by introducing a second type of loop - the while loop.

2. While loops - syntax

The while loop is very similar to the for loop, but with some key differences. The for loop is one of the most common loops that you will encounter in Julia, and the while loop is typically less popular. However, in certain cases it is still a powerful tool to have available. One use case for the while loop is to repeat an expression continuously until the condition is met, as opposed to our for loop previously, where we primarily used it to iterate over the length of a data structure. Looking at the structure of the while loop, we repeat the expression within the loop, until the condition is no longer met.

3. While loops - example

Let's look at an example of a while loop. In the while loop below, try identify the condition and the expressions. The condition is the second line - where the variable counter is not equal to zero. The expression is contained within the while loop, so it consists of one println statement where we print counter, and then we decrease the value of counter by one. Note that before this loop, we had to declare the variable counter, and we have set it to ten. What do you expect the output of this code to be? If you guessed that it would print the numbers from ten to one in descending order, you're correct! The final output is shown on the screen. Let's step through this loop for a few iterations.

4. While loops - example first iteration

The first time Julia encounters this loop, counter is equal to ten. So the condition "while the counter is not equal to zero" is met, and we then move on to evaluating the expression within the loop. We print the value of counter, which is ten, and then we decrease the value of counter by one, meaning it is now nine.

5. While loops - example second iteration

The second iteration continues on from the first, except the big difference is that counter has had one subtracted from it, so it is now equal to nine. The loop evaluates the same - passing the condition, and then evaluating the expression, printing nine to the console, and then subtracting one from counter, setting it to eight.

6. While loops - example third iteration

The third iteration continues on from the second. The counter is now equal to eight, and so eight is printed to the console. This continues on and on until counter is equal to zero, at which point the condition is no longer true, and the loop terminates.

7. While loops - infinite loop

It is important to note that this loop did indeed terminate in the example shown previously, as we kept decreasing 1 from the counter value until the condition was met. But what would happen if we had forgotten to decrease the value of counter in each iteration of the loop? We have demonstrated that situation in the code here. This leads to an infinite loop, constantly printing the first value of counter, 10, to the console, as the condition is never met. It is very important to remember to test your loops thoroughly so that this does not occur!

8. While loops - termination

In the DataCamp environment, an infinite loop will disconnect your session and you will need to reconnect. If this happens on your own local machine, you can use the keys Ctrl + C to force terminate the Julia script.

9. Let's practice!

Now that we've seen how while loops work, let's work through some examples together and write our own.