Check out a free preview of the full Java Fundamentals course:
The "Nested Loop" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses nested loops as a loop inside a loop where the inner loop will execute once for each iteration of the outer loop. A student question regarding how to decide how deeply nested loops should be is also covered in this segment.

Get Unlimited Access Now

Transcript from the "Nested Loop" Lesson

[00:00:00]
>> So this one says, find the average test scores for each student in the class. So you would then need one loop that's gonna go through every student in the class. So there's however many of you in here, that will be one loop. And then go through every test grade that the student has, that will be another loop that goes through the tests, all right?

[00:00:22] So this is what the code will look like for something like that. We say we have 24 students, they have 4 tests each. On line 16, we have that for loop that says go for the number of students. This is called an outer loop, right? And then inside of that loop, we then add another loop on line 21, and this is for that particular student's tests.

[00:00:51] Notice here, on line 16, we initialize i as 0, we use i, cuz that's convention. If you're gonna use an inner loop, do not use i again, cuz i is tracking that outer loop. You're gonna get yourself royally confused if you try to reuse that in another loop inside of there.

[00:01:11] So convention is, what's the next letter? j, we use j for the next loop. If you had another one, you would use k or whatever, but you should rethink life if you get three loops in. [LAUGH] All right, so that will process the number of students, this is what that inner loop would look like.

[00:01:31] Notice here on line 22, I said Score for Test #, and then in parentheses I put j+1. What is that doing, anyone know?
>> It's printing where we are on the loop, but since it's 0-indexed, it would be kind of odd for the user to read that.
>> [LAUGH] Exactly, so we start loops with a 0, that's a programming computer kinda thing, right?

[00:01:55] In real life we don't talk to people like that, what's test score number 0 like? No one says that, right? So we're just adding this for a little user friendliness, a 1 will be added to j. It's not actually added to j itself. We put it in parentheses to say, just temporarily add 1 to whatever's in the value of j, and print that out on the console, all right?

[00:02:22]
>> So it's displaying it, but it's not assigning it.
>> Yes, exactly. Okay, so this would be that whole program. So 16 is the outer loop, 21 is the inner loop. Notice 27 goes on to do more things. It is still inside of the outer loop, okay? So this outer loop has statements, an inner loop, and then more statements.

[00:02:49] So that covers loops, any questions? How do you feel about that? Feel good? All right.
>> I do have one question.
>> Yeah.
>> You were talking about if you had multiple loops inside of loops inside of loops, and how it's not a good way to go about it.

[00:03:09] What convention would you use-
>> [LAUGH]
>> If you have a less naive way to go about something like that?
>> Yeah, I mean, that really depends on the situation. Sometimes you have no choice, you just kinda get yourself in that. Two loops is not abnormal, right? But as you start getting deeper and deeper, even the two loops, you start having performance implications with that, right?

[00:03:33] So there's ways, and it's beyond the scope of this class, but you'll start thinking of how else might I do this to have a shorter time complexity and stuff like that. And it just might be a way of restructuring. I can't give a hard answer cuz it really depends on whatever it is you're trying to do.

[00:03:56] But methods is one way where you might have this code inside of another method. So that's another way to not duplicate stuff, and you can just call out to it when you need it.