Start learning today, and be successful in your academic & professional career. Start Today!

• ## Related Services

### Start Learning Now

Our free lessons will get you started (Adobe Flash® required).

### Membership Overview

• *Ask questions and get answers from the community and our teachers!
• Practice questions with step-by-step solutions.
• Track your course viewing progress.
• Learn at your own pace... anytime, anywhere!

### Conditional Loop Wrap-Up

• Any conditional loop can be nested within another as many levels deep as necessary.
• continue statements can be used in nested loops to execute a continue on ‘outer’ loops by specifying how many levels of loops to run continue on.
• break statements can be used in nested loops to break out of ‘outer’ loops by specifying how many levels of loops to break out of.

### Conditional Loop Wrap-Up

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

• Intro 0:00
• Lesson Overview 0:09
• Lesson Overview
• Nested Conditional Loops 0:39
• Nested Conditional Loops
• Coding Example
• continue & break Re-Visited 5:17
• Continue Statements and Coding Example
• Break Statements and Coding Example
• Loop Debugging Tips 15:16
• Add Short Debug Statement At the Very Beginning
• Output a Counter Variable
• Add Debug Statement At the Very End
• Homework Challenge 20:20
• Homework Challenge
• Homework Challenge (cont.) 22:00
• Homework Challenge (cont.)

### Transcription: Conditional Loop Wrap-Up

Hello again, and welcome back to Educator.com's Introduction to PHP course.0000

In today's lesson, we are going to be wrapping up our discussion of conditional loops.0005

First, we are going to be talking about nested conditional loops, just as we have talked about nested conditional control structures.0011

You can do that with loops, as well.0019

We are going to revisit the break and continue statements, as they relate to nested conditional loops,0021

because they are a little different application when loops are nested.0031

And then, we are also going to go over some loop debugging tips, because loops can sometimes be tricky to get them to work right.0034

So, first of all, any conditional loop can be nested within another conditional loop, just as was the case with our conditional control structures, like if.0042

One of the common uses for nesting conditional loops is to loop over multidimensional arrays,0052

because that allows you , in the outer loop, to iterate over one dimension of the array;0061

and then in this inner loop, you will iterate over the second dimension of the array.0067

Let's take a look at that in an example.0071

This is a file called nestedLoops.php, and we have created an array here called matrix.0074

It is a multidimensional array; it is supposed to represent a 3x3 matrix.0081

And the outer array is a numerically-indexed array that contains three arrays, each of which is a numerically indexed array0086

that contains three elements, which are just the strings...for the first array (used to represent the first row of the matrix),0097

1a, 1b, 1c; 2a, 2b, 2c for the second row; and so forth.0104

And all we are going to do, to practice with the nested loops, is to iterate over this array and output each row.0109

The way we do that is: the first thing we are going to do is iterate over the outer array, which is going to be matrix itself.0117

And we are not using a foreach loop here, even though we are iterating over an array,0127

because I want to talk a little bit about using counter variables in nested loops.0132

And I will get to that in a second; but basically, this foreach loop right here loops over the outer array, which is the matrix variable.0138

And once it iterates each time on the matrix array, we are going to set a variable called row equal to whatever value we are on as we iterate through the array.0150

So, the first time through, row is going to equal this array here; the second time through, it is going to equal the second array; and so forth.0164

And the way we access that is by using the counter variable to get access into this indexed array.0170

Then, what we are going to do is, using a while loop (and this is to show how you can nest different loops within other loops),0180

we are going to loop over the row array, because each element of matrix is an array itself.0187

And we are going to loop over each row array and output each of its different values.0195

So, what this does is: we set up a counter, 0, and this is our test condition to make sure that we are not looping past the end of the array.0202

We increment the loop counter at the end of each while loop, and then the content of the loop is0213

that we are just going to output each row value, followed by a space.0217

Now, here we are also iterating over arrays, so typically you would use a foreach statement for that.0221

The reason is just to show that you will often see, especially when iterating over multidimensional arrays, several levels of nested loops.0228

And it is common in the outer array, when you have a purpose for using the counters and wouldn't be using a foreach loop,0236

that the outer array (and this is kind of a convention that is used)--the counter is called i.0243

And then, for the next loop nested within that one, the counter is called j; and then, if you have a third one, it is called k.0247

And so, it's i, j, and k; and that is a common convention.0257

I just wanted to explain it to you: you will see that a lot--that when you have multiple conditional loops nested within one another0260

that have counters, the common convention is to name them i, j, and k.0268

And you typically don't see more than three loops nested together at the same time.0272

If we go ahead and run this script, nestedLoops.php, we can see that what it does is: it goes through and, in the outer loop,0277

it pulls out each row of the array, or each row of our matrix, and then loops over that array, outputting each of the values in it.0288

So here, we can see all of the values in that first array, which is the first element of matrix.0297

And then, you can see that it has done the same for the second element of matrix and the third element of matrix.0306

And so, that is one way that you can use nested loops to loop over a multidimensional array.0312

Now that we have talked about how there can be nested loops, there is a variation0322

that you can use on the continue statement, and the break statement, as well.0327

First, we are going to talk about the continue statement.0331

And because continue, as we know, says, when you encounter it within a loop, "Skip to the end of the loop and start the loop over again;0333

re-evaluate the test condition"; well, if you have nested loops, you can specify the number of loops which you want to continue on.0343

So, if you are inside the inner loop of an outer loop, for example, you can specify continue 2, with the integer 2, which says,0350

"I don't want you to just continue on the current loop, but I also want you to continue on the outer loop, as well."0358

That will make a little more sense when we look at an example; and you can use that for as many levels deep as you have.0363

So, if you had four loops nested within one another, on the inner for loop, you could type continue 4.0368

And that would say, "Go all the way back to the outer loop and begin looping over that one again."0374

So, let's take a look at a file called nestedContinue.php.0381

It contains an array called numbers, which is an indexed array containing three indexed arrays, each of which contains a couple of random integer variables.0384

And what we are going to do is loop over the numbers array, extracting on each iteration the value of each element, which is going to be an array itself.0395

And we are using here, instead of the foreach statement, the for statement, for formatting reasons.0408

I want to output breaks after each row, and so what I do is: I don't want to do that for the first row, so I use the counter variable0417

to see if you are at the first iteration of the loop ("do not echo the break statement").0428

And so, that is why I have used the counter loop here, even though we are iterating over an array.0436

What this does is iterates over numbers, and then we define a variable, numberSet,0441

which we set equal to whatever array it is currently being looped over,0447

in whatever element is currently being looped over numbers, which is an indexed array itself.0452

And then, for each number set, or each array that we have here, we are going to loop over that.0458

And here, we are using the foreach statement, and we are saying, "As we loop over numbers set, I want you to pull out the value0463

and set the variable currNum equal to that value."0470

So, as (for example) the inner loop iterates over this first array, currNum is going to get set to 1 the first time around.0475

It is going to get set to 2; it is going to get set to 3.0482

And what we are doing here is: inside this inner loop, we are going to output the current number, unless...0485

if the current number that is in the array is greater than 10, we are going to use the statement continue 2.0493

And what that means is to quit outputting numbers for this current row and go back up to the outer loop (which says to pull up the next row).0500

So, what it is going to do as this functionality occurs: it is going to first loop over numbers; it is going to pull out the array that has 1, 2, 3.0512

And then, in this inner loop, none of these numbers equal 10, so it is going to output them all.0523

Then, on the second iteration of the outer loop, it is going to load this array into the variable numberSet.0527

Now, when it goes to iterate over this and output it, it is going to find that that first number is greater than 10.0532

So, what it is going to do is execute the continue 2 statement, which means, instead of continuing foreach0540

and going ahead and outputting the rest of this array, it is going to say "skip to the outer loop and continue."0545

So, it is going to skip and then begin outputting this next row.0551

Now, the next row--the inner loop is going to do the same thing: it is going to output all of the numbers, until it finds a number greater than 10.0555

And if it does, it is going to continue on the outer loop, because when it continues on the outer loop, after this third array,0561

there are no elements in the array left; we just jump to the end down here.0570

If we go ahead and run this script, nestedContinue.php, (and this is our array that we have here:0574

this is just an outputted form of the array, so that we can look at it when we are analyzing the output),0584

we can see that the first row is completely output: 1, 2, and 3.0591

Now, the second row actually is a blank line: nothing gets output, because the first number it reaches is the number 100, which is greater than 10.0594

So, what it is going to do is jump back out to the outer loop, move on to the next row, and start outputting the numbers in that row.0602

In this case, it gets to 6; it gets to 5; then, when it reaches 11, it hits that continue statement again and jumps out to the outer loop.0609

There is no more; so 11 doesn't get printed out; 4 doesn't get printed out; and that is the end of this numbers array, so that is all the output that happens.0618

Then, notice the difference between that and if we just had a regular continue statement.0625

The regular continue statement is the same thing as if you did continue 0 or continue 1; but the way you will always see it is just a continue statement.0630

Now, what this is going to say is: as you are going through each row of this numbers array, if you encounter a number greater than 10,0638

rather than continuing on to the next row, just continue on this loop right here,0646

which is going to say, "Just continue on to the next number."0651

So, in this case, when it reaches 100, instead of jumping to the next row and beginning to output that,0653

it is going to output 7 and 3, and then when it reaches 11 in this row, it is going to continue the loop and output 4.0658

If we save this file and reload it, we can see that that is what happens.0666

It outputs all three numbers in the first row; in the second row, it skips over 100, but instead of skipping to the next row,0671

it goes ahead and just continues in that inner loop; it outputs 7 and 3.0677

And then, the same thing happens in the last row: it outputs 6 and 5, and then skips over 11 and outputs the number 4.0681

So, that is how you can use continue in nested loops to jump out or to continue multiple loop levels.0688

Likewise, the break statement works in a similar way with nested loops, in that, when you have nested loops,0695

you can specify break 2, break 3...and that has the same functionality as break, but instead of breaking0700

out of the current loop that you are in, if you say break 2, it is going to say "I want you to break out of the second loop from where I'm at."0707

So, if you have an outer loop and an inner loop, and you are in the inner loop, and you hit break 2,0713

it is going to jump out of both of the loops altogether and jump to the end.0717

The way you do that is: you specify how many levels you want to break out of.0721

And that could be as many as you need it to be, because you can nest loops indefinitely, or as much as you need to, within PHP.0726

If we go and look at a file called nestedBreak, we have the same thing here.0735

We have the numbers array, except we have replaced the second number in the second row with the value null.0740

Again, we are going to loop over this numbers array, each time setting the variable numberSet equal to0748

the value of the numbers array that is currently being iterated over, which is an array itself.0759

We have used the counter here again, for formatting reasons: we want to test if it's the first row that we are outputting.0765

Then, the change down here is that we are going to loop over each row, each number set (which would be like 1, 2, 3, 100, null, 3),0774

but instead of our (actually this is incorrect...a typo) testing to see if the number is greater than 10,0785

we are going to test if we encounter the value null.0796

If we encounter the value null, then we are just going to break out of all loops altogether.0799

And because we hit break 2 here, instead of...if we had just done break, we would have just quit looping over this row0802

(in this case, it would be the second row) and then moved on to the third...we are going to break out of everything altogether.0811

So, as soon as we hit this null, we are jumping out of both loops, and we are going to stop.0816

So, 100 will be the last number that is going to be output.0820

If we go ahead and run this script, nestedBreak, we can see that what happens is: it outputs 1, 2, and 3 right here, in the first row.0824

Then, it reaches 100, and then afterwards, it hits null.0835

And instead of continuing...not only on this row, but not on this row either...it breaks out of both loops.0839

It just completely jumps out of the loop, because we specified break 2; the last number output is 100.0844

And the break works the same way as continue; you could specify break 0 or break 1; those are both the same meaning as just the break keyword itself.0853

Now, what is going to happen here is: we just have a single break--it is not going to break out of two loops.0862

So, any time we encounter null (for example, in this second row), instead of breaking out of both loops,0869

we are just going to break out of the inner loop.0875

So, we are going to reach null, and we are going to stop processing this row and jump out to the next one.0877

So, instead of completely quitting iterating over this numbers array, we are going to jump to the next row.0882

And we will see how that change looks.0889

You can see, what happens is: it output the first row; it started to output the second row.0893

When it hit null, instead of breaking out of both loops, it just broke out of the inner loop, which was the output of this row.0897

It moved on to the next row, and then it went ahead and output all of those.0903

That is how you can see how to use the break statement in nested loops to jump out of multiple loops.0907

Now, I just want to talk about some loop debugging tips, because loops can sometimes be tricky to debug.0917

And one tip is similar to what we used for debugging our conditional statements, where at the beginning of each if statement,0922

for example, we would say "within if" and have a little short debug message0930

that lets us know that we entered that particular section of code.0935

One thing that you can do is put a short debug statement at the beginning of all of your loops.0938

And that will let you know every time you enter that loop; so, if you know that, for example, you have a loop0944

that you know is supposed to iterate 10 times, or you expect to iterate 10 times,0949

and you have a debug statement at the beginning of that loop, that debug statement should be output 10 times,0953

letting you know that you entered that loop 10 times.0958

If it is not, that gives you a signal and a clue to figure out where your problems may be.0959

If we take a look at a script called loopDebug.php, we have the same array that we had in our last file.0965

It is this multidimensional array, and it has the same functionality that we just had, where it is going to break out if it hits a null value.0972

The difference is that we just added some debug statements.0982

So, at the beginning of the outer for loop, we had a debug statement that says "enter the outer loop."0985

And then, every time we loop through and enter the inner loop, we are going to say "debug: enter inner loop."0990

So, if we run this script, we can see our debug statements.0996

And we are going to see that they match up with what we expect.1005

First of all, we see the first one that it encounters is "enter outer loop."1008

That makes sense: the first thing we are doing is entering that outer loop to start iterating over the numbers array.1012

Then, we are entering the inner loop, which means we are iterating over this second-level array.1018

Then, we have output the number 1, and after we have output the number 1, we continue to iterate over that second array, or that inner array.1025

We output the number 2; then we enter the loop again to output the number 3.1033

Once we output the number 3, this inner loop is going to quit,1040

because the condition is going to be met that you have reached the number of elements in that array.1044

So, that is going to stop and jump to the outer loop.1054

So, the next thing you are going to see is that you have entered the outer loop again; so we know that we have entered the outer loop.1057

Then, we enter the inner loop, and we output 100, which is the first one.1061

We go ahead and try to enter the inner loop again, but what is going to happen is: the second time we enter it,1066

we are going to reach this break statement that is going to happen because this is going to be equal to null,1073

because the second value in that array is equal to null.1078

So, we are going to break out of it and break out of all the loops altogether.1081

And you can see that entering that loop is the last thing that gets output.1084

And as you can see here, we have the counter variable for this outer loop.1106

Also, for this inner loop, I have changed it from a foreach loop to a for loop, so we have this counter variable j.1111

And I am just going to append to the debug statement we currently have, and just say, "counter=" and then output the value of the counter.1117

Same thing down here, except the counter value is j.1134

Now, if we go and refresh the page, we can see a little more information.1142

We can see, "OK, we are entering the outer loop; at this point, the counter is equal to 0."1146

"We enter the inner loop now; the counter is equal to 0; we output the number 1; we enter the inner loop again; now the counter is 1."1150

And then, finally, the last thing--another thing you can do--is add another debug statement to the end of each loop1162

to make it even clearer when you are exiting a particular loop.1167

I have already had those statements here at the end of the for loop: every time we exit out of it, I just have a statement1173

that says "end inner loop," and then a statement that says "end outer loop" every time we exit the outer loop.1178

Again, it just adds a little more information: it may be a little overkill, depending on the circumstance.1185

But this is just showing you some of the tools that you have available to yourself as a programmer to try and debug your loops.1189

And so, you can see here, it enters the outer loop; then it enters the inner loop and prints out the number 1, and then it is exiting the inner loop.1197

And then it enters the inner loop again and exits the inner loop, and so forth.1205

And it does so--those statements just trace how your script is flowing through all of the different loops in your script.1208

And so, those are some useful debugging techniques.1217

For the homework challenge, I want you to define a multidimensional indexed array called matrix.1221

And I want you to use a while loop, nested within a for loop, to populate the matrix with three indexed arrays.1228

It is going to look just like the matrix we had in our examples.1235

It is going to be an indexed array; it is going to be called matrix; and it is going to have three arrays within it as its values.1242

And then, in each of those arrays, I want you to add the numbers 1 through 3.1250

It is going to be an array filled with three arrays, each containing the numbers 1, 2, and 3.1254

Then, I want you to use two for loops to iterate over this matrix that you have created, outputting the values in each array in matrix.1259

For each row of that matrix array, I want you to output all of the values (1, 2, and 3 for each row).1270

However, I want you to prepend each number that you output with the row number and a dash.1277

For example, when you are iterating over matrix, and you are outputting the numbers in that first array1283

(which would be 1, 2, and 3), I want you to prepend the 1- to each of the different outputs.1290

And then, when you are on the second row, you output 2-.1297

And so, your output should look like this right here.1300

That is the desired output; so if you run your function, that is the output that you should get.1307

The next thing I want you to do is to change the number 2 in your second row to null.1311

This second one is going to become null, and what that is going to do is: we are going to get some practice with the continue and break statements.1316

So, using any conditional loops that you wish, I want you to output each row of matrix, just as we did in step 3,1326

except that when you reach the null value in row 2, I want you to break out of both loops.1334

And so, that is going to give you experience with using the break statement to break out of multiple loops at the same time.1339

Run the script and verify that as soon as you get to the second element on row 21345

(which is going to be the value null), that you jump out and your output halts.1351

Then, change the loops so that, instead, when you reach the null value in row 2--instead of completely jumping out of both the loops1356

and quitting output altogether, you just skip outputting the current row and move on to the next row.1364

When you run your script again, you should verify that rows 1 and 3 are completely output.1370

But then, row 2 only has the value 2-1, because when you reach 2-2, it doesn't exist anymore.1375

It is the value null, and you skip to the next row.1381

And that is going to give you experience with using the continue statement.1383

That ends today's lecture; thank you for watching Educator.com--I look forward to seeing you next time.1388