From the course: Accelerated MATLAB

Nested loops

- [Instructor] As covered in the Learning MATLAB course, loop control statements can repeatedly execute a portion of code. Let's review a simple loop code using the for statement. We initialize a variable which is the total sum to store the sum, starting at 0. The for loop runs from 1 to 10. The loop variable i takes on each value in this range one at a time. In each iteration, the current value of I is added to the total sum. After the loop has finished running, which means after it has gone through all numbers from 1 to 10, the total sum can be displayed using the disp function. Now, let's take a look at nested loops. We are going to add an inner loop to the previous example. The outer loop iterates over each element of N_values vector, which is specified as having three values, 5, 10, and 15. The j operator is used to access these values. For each iteration, N is set to each value and the total sum is every time initialized to 0. The inner for loop runs from 1 to N. The variable i takes on each value one at a time. In each inner iteration, the current value of i is added to the total sum. As you can see here, for visibility reasons, the nested loops are intended. Now, let's take this example and write it in MATLAB. So let's go back to MATLAB. Let's create a new live script and add the code that shows the example from the slides. So let's create a new live script here. And for the sake of time, I'm just going to paste the code, but I have added disp(j) and disp(i) at the end of the nested loops. So if we run this code, after the loops have run, we can see the values of j and i here. Now, let's take another example that uses nested loops with for, continue and break. This example will generate a multiplication table for numbers from one to five, but skipping the multiplication by two. At the end, we are going to display the product using the conversion from numbers to string command. So let's open a new live script here, and we are going to generate a multiplication table. So let's start with the outer loop, for 1:5, and this will be the outer loop. We will have the inner loop, for j 1:5, for the inner loop. if j == 2, then we continue, which means this will skip the multiplication. And then end. We are going to type product here. So product will be equal to i multiplied by j. And if this product is higher than 20, then we break it. And this means that we stop the inner loop if the product exceeds 20. Then we end this, and now we have to display. So we can use the display function here, and have the num2str of i. And I'm going to use space here and multiplication with another space, and another space here. So we'll have, again, num2str for j, and also another space here. That will give us the equal sign. And one more space, num2str for product. And then we'll have end here and another end at the end. So this will be our code for the multiplication. I think I am missing the i variable here. So for i = 1:5, and then you can see how the error sign disappears. So let's take a look at this by pressing Run. So then we can see all the multiplication table. But as we go forward, you can see that on the j side, so for the inner loop where we actually broke the multiplication table, so we skip the multiplication by two only for the inner loop so that you can see the numbers here on the right side that represent j. You go from 1 to 3, 4, 5, 1, 3, 4, 5, and so on. But you can still see 1, 2, 3, 4, 5 on the other side for i, because we only skipped multiplication by two for j. In the next video, we will dive into the switch, case, and otherwise statements.

Contents