1. For loops
Hi, my name is Anthony and I'm a Quantitative Developer based in Sydney, Australia. I'll be your instructor for this Intermediate Julia course, and it is my pleasure to welcome you to what will be a fun and engaging learning experience. In this course, we'll be able to take your Julia skills to the next level.
2. Course outline
You'll learn about control structures in this chapter, which are a very important part of not only Julia, but all programming languages.
Throughout this course you'll also learn how to store data in new, advanced data structures, how to use this data in custom functions for reproducible code, and how to work with and modify data sets that you'll come across in practice.
3. For loops - introduction
We will start with a for loop. The for loop allows us to repeat an action a known number of times. You can think of a for loop as similar to an if statement - the code will be executed within the loop, but only if certain conditions are met.
Take a look at the basic structure of a for loop.
For each variable in an iterable, execute the expression.
Let's see how this actually works.
In the vector shopping_list, we have a few things that we need to buy from the supermarket. If we wanted to print this vector, we could use println.
4. Repeating single commands
However, what if we want to print each item separately? We can use four separate println statements.
This is cumbersome, tedious, and for larger vectors will quickly become impossible. Instead, we can use a for loop.
5. For loops - the structure
Take a look at this for loop.
The iterable is our shopping_list vector. This is what we are iterating over. Our iterator is the variable item, which will iterate through that shopping list. In each pass through the loop, we print out the value stored in the iterator, which is the item in the shopping list. The end statement denotes the end of the loop.
Note that our iterator item is just an arbitrary variable name - we could call it anything, but it's good practice to call our iterator something related to the iterable.
At the conclusion of the loop, we get the following output.
6. Iterating through the shopping list
Let's follow the execution of our for loop through each step. When we run our code, Julia will see this for loop, and begin to evaluate it. It will see that shopping_list is a vector with 4 elements, and then it begins to iterate.
In the first run, Julia stores the value 'Apples' in the variable 'item', and prints it out.
7. Iterating through the shopping list
Once the first iteration is complete, Julia goes back to the top of the loop. In this second iteration, Julia stores the value 'Bread' in the variable 'item', and prints it out.
8. Iterating through the shopping list
then Carrots
9. Iterating through the shopping list
The final iteration stores Strawberries in the item variable and prints it out. Julia recognizes that it is at the end of the iterable, and terminates the for loop.
10. Enumerate
You've seen how we can get the values from a vector using a for loop. But what if we also want to get the index that matches that value? We can do exactly that using the enumerate function.
The enumerate function yields two values, and in the example we have named them index and item. The index, starting at 1, is the index of the item that is returned. The value stored in item is the value in shopping_list at the index value.
Let's look at this. Note the index first, and then the value. Comparing this to our shopping_list definition, we see that the output of our loop is correct - Apples is indeed the value at index 1, and so on.
11. Let's practice!
Now that you've seen how powerful the for loop can be, let's work through some examples together.