Teaching my 40 year old student with no experience programming | For Loops

Phil Choi teaches a novice programmer the mechanics and use-cases of for loops and while loops. The transcript shows step-by-step explanation of loop initialization, condition checks, incrementation, array traversal, mutating an accumulator (total count) with "+=", and pushing computed values into another array. It also covers differences between for, while, and do-while loops, warns about infinite loops, and mentions a homework exercise to iterate an array with a while loop and compute statistics such as median.
Chapters
All right. The 300. You see that?
Steep discount. I like it. I'll take it.
That's a, that's a money glitch right there. Right? Very simply, like, let's say there's numbers, right? 1, 2, 3, 4, 5, 4. Let I equals 0. And while I is less than numbers dot length. So right now numbers, dot length is five, right. And I is zero, right? So you're saying while I is less than numbers length. So while I is less than 5, do this code down here. Each time you run this block of code, I'm going to add one to I. Right? Gonna run one for zero. It's going to run one for one because one is still less than five, right? And it's going to run one for two. Two is less than five. Three, three is less than five. Then it'll run four and that's going to be five times I will become five. And then is five less than five? No. Right? No, then it'll, it'll end the code right there. That's how I, I think about a for loop.
Okay.
I have a total count and I, I equal that to zero. And then I need to, I need to make total count equal total count plus 10. And I need. And by the end, let's do it the right way. All right. What would you expect total count to equal at the end of this whole thing? So the first time I is zero. We're going to add 10 to total count, Right?
Right.
So now.
Okay, I got it. So it's not, it can't be. It's not one plus at this total count. So then it's going to be 50.
Exactly. Exactly. Okay. Yeah. You got it?
I got. Yeah, yeah. Okay, I see where I was thinking plus one and then I plus total count. But we're doing, we're just, we're only increasing total count. That's it.
Exactly.
And we're at the same time looping through. Where's at this point, though? Like, I know we have I here, but where is that? That number being like stored.
I is stored here because it's a lead variable, right?
Okay. But we have zero and we start at zero. Then it goes. And it automatically just updates.
Yes, it updates here in this incrementer. And it goes back, goes back.
And now it says, hey, we let. We're eyes now one like you said, the same through. It goes through that loop four more times before five is eventually five less than five. But by that time we've added.
So it goes through like this is its own little piece of code, right? It goes, hey, I0A is I less than five? Oh, okay, it is. All right, plus, plus. And then it goes, okay, I is now one is less than. So this is its own little track, right? And then whatever's in here is its own little track. But right here, you can use I inside of this block of code as well. So, like a good way, I'll just go into the for loop file. So I just want to show you like, For Let IFor 0 I and let's make that range. All right, so that's how our code looked earlier. So if I. If I show you the incrementer, it's going to go 0, 1, 2, 3, 4. Right?
Right.
And then if I want to use this index here. So this log will go 0, 1, 2, 3, 4. And then we can traverse or kind of go through this array, right? So it'll go 0, 1, 2, 3, 4. So let's try this code out. So here you see that I is zero, right? I is zero. And so it's. And then I'm asking, okay, print out range I. So range I would be range zero first. Right? And that's one, right? And so on. And then I becomes one. And so this is kind of like how you can kind of traverse through it. And so say I want to do something like add to range. Okay. So I start off like here, and then I say add to range dot, push on the range of I +2. So in the end. All right, so what do you expect add to range to be here if you had to read it?
So the first one's gonna be three.
Okay, that's correct.
And then after that, then three, then four or five. Six. Three, six.
Yes, four. So it'd be three.
Wait, six, three, six, then six.
Wait, now I'm getting my math wrong.
Hold on. Yeah, I don't know.
One plus. Oh, yeah. So it'd be three, four, five, and six. Correct.
Wait, what?
Three, four, five, six, seven.
I thought we're adding two each time.
So we added two to each element inside of the array.
Ah, I don't know.
I don't know. I see where you're getting confused, because when. When I've added two, maybe you expected the. The two to get added onto the I. Yeah, like right now we're not touching I. Right. Like, if. If that was the case, we'd say plus two here. Right?
Right. And I think that's what it is because so like, I in itself is independently running. And then now the 2 range is saying, hey, what's what's I status right now? And it's going, oh, I3. Okay, well then give it two again. And then it loops again. And then I independently is plus one, the I plus plus and it runs and it's still underneath the range. And then add to range again. It's going, hey, what's the status of I? Oh, it's two now. Okay, great. Let's add two to it. Now we got four.
So then it's. That's perfect, actually.
Okay.
Yeah, that's perfect.
Damn. Okay.
Yeah, I see what you're saying. Like we're going to add to range, right? Which is just adding on something onto the end of the array. So right now we're at. Now we're saying like, hey, what. What are you I. Right?
Yeah.
And I is like, hey, I'm right now I'm one, which in range is two, right? I plus two here. And that's how we get the new value. Right. I think that's what you're. You're thinking. Yeah.
Yeah.
Okay. It's. It's pretty simple, right?
That one is. But nothing else.
But for loops and conditionals are like the bread and butter of coding. Like, for loops is how you're going to, like, on a website. No one's gonna like put HTML like a hundred times for like, I don't know, like Facebook, like all the comments and stuff. I think there's comments in Facebook. I don't know, like all the. Yes. So like, usually, like the posts, it'll go through a for loop of like the object, the data structure. So it'll be like, it'll be like, oh, what are you thinking today? Or like that post title. And then it'll have comments and likes. It'll iterate over it and It'll be like, okay, 0, 1, 2, 3, 4, 5. And then it'll display it on the page. So the for loop is very powerful with an array of objects.
Okay, and just. Oh, real quick, can you go back to the ReadMe page that we had? The plus the. Okay. I just want to make sure. Like, I thought the. I wanted to make sure that that total count was let and not okay const.
Because I was like.
Yeah, I just wanted to make sure. Like. Okay.
And then like here, if you put cons, it would. It would totally break it as well.
Yeah, it's not going to work.
Right?
Because it's going to keep going. Hey, you can't change it.
Yes.
Okay, cool.
All right, cool, cool, cool. All right, how about for loop? But I feel like we're gonna have to drill it more in next time.
Yeah, yeah, definitely.
Let's go over this stuff next time. And I think we can go through for loop and while loop on Friday. While loop is very similar. It's like while a condition. Keep doing it until that condition. And then. So you do have the if else now for sure. And then the switch case. I. I want you to do this exercise here and just message me if you need some help.
Okay, Dan? Yeah. Thanks again. And I guess now everybody saw my homework. And so I'll look and see if anybody posts their homework. And so I can look. We can compare contrast type of thing. So that'll be cool.
All right, cool, cool. Thank you so much for your time. And I'll see you on Friday.
All right, thank you. See you Friday. Have a good one. Bye.
Can you kind of fill in the blanks here? So it could add 1, 2, 3, 4, 5 together?
So it can add 1, 2, 3, four, five together?
Yes. So we have an initialized total count up here. Right. So the first thing I want to do at step one, let's just write it out here in pseudocode. I want a total count plus equals on one. Right. And then next I want to do total count plus equals on two. Or I want this. This is just short, short syntax. I think we went over it. It's just like a plus plus. But you can do like other numbers. Yeah, yeah. So you can actually do the one too. So total count plus equals on three. Total count plus equals on four. Total count plus equals on five. So I am incrementing this at the index of numbers at 0. Numbers at 1. Numbers, 2. Numbers, 3. Numbers at 4, 4. So these are kind of the steps that I'm going through. So I Wanna. I started 0. I want to add 1. 1. I want to add 2 to the 1. Now that's 3. I want to add 3 to the 3, 6. Now I want to add 4. And there's 10, and then it's 15. Right. So how would I go about that within a.
For loop? I don't have a clue because, like, I'm looking at it and it's zero. I know it's less than five. I know it's gonna keep looping from here. Like, okay, yeah, it's gonna loop. But in my mind it's just like.
I don't know.
Go like, do it. I don't know.
That's very close, I think. Yes, it's. I. I honestly just want to say, like, really obvious. Just do it, you know?
Yeah. But I don't know what. I know that's not it. So.
Yeah, so I think it would just be total count plus equals on the global variable numbers. And then I.
That's what it is. Okay, so we have to start. We got to start somewhere. And we're going to start with. Because I is zero and so it's going to start at here and it's going to go, hey, we're zero, it's less than five. Okay, let's add it. Add plus one. And then now our total count should be one.
Yes.
No, wait, no. One. Yeah, one plus one.
One plus zero first because we have.
Oh, sorry. Zero. Yeah, one plus zero. So we have one and I is.
Still less than five right after.
Right?
Yeah, zero. So you go back one.
So now I got one and again less than five. So add again two and then I'm gonna take my total count, which was one the first time, plus two now and I should get three. But I'm not great at math after that. So then I should console log something consol. And this should tell me like after each time it runs through, I should see it like increase.
Okay, let's try it out. So let's just make a nice for loop js copy paste that there and. And follow me into. For loop JS. So we need the numbers too. Let numbers equals 0 or 1, 2, 3, 4, 5. And then maybe we could refactor this to say numbers length. Let's just see what's going on here. I'll just log everything right now.
Right.
We have not mutated. We have not added anything from total count yet. Right. Because. Because it's running line by line.
Right, right, right.
So we're only. We're adding to total count at the end on line nine. So these logs are up here. So the on the first loop is 0, 1 and then he's 0. Right. Total count is 0 right now. Right. Right here. Total count is 0. Right on line 7. And then the next, the next loop, total count is different. Right? Because we're doing it at the end. Or maybe this will make more sense.
Can you move that one back to where it was? So it's like. I think it's just because we're starting with one. So like if it was six, I'd be okay. I zero. Hey, we're in array zero and that number is six. Our total count is zero. So what's zero plus six? Six. Let's loop I go back.
Okay, maybe this will make more sense too.
I mean, but is that right? That's what's going on.
Yeah. Yeah.
Okay.
P4 plus total count gets added. Something added to it. Total count is zero right. Before we add and then it adds. And then after total count is one at the end of the loop. Right. And then we have the let it loop. And then Now I is 1, the next number is 2. Before we add anything to total count, it's 1. And then after we add it's 3. Right, right. All right. And then so on. And it keeps going and keeps going. And we could add. We can add like a bunch of stuff here.
Because it's just going to keep going because we have numbers, not length. Okay.
Code. Right. So let x =0 while x is less than 5. I want to increment X. Right. So then at the first one it'll be like X is 1, 2 and then 4 and then at 5 it'll break. Right. Kind of like a for loop. It's the same thing. So there's just two different ways to write it. Before loop does have a little bit more rules like right in your face, I think. And while sometimes it gets a little crazy, you get caught in an infinite loop all the time. Like a lot of games, right? Yeah, I'll use a while loop like while and then. Because you just have to keep running it, right. So it's like while true. And then it's like if. And then it's like if player move equals, equals left, you know, and then it'll. It just keeps running the code like it's in a. It's in a forever loop. You know what I mean?
Yeah, yeah. Because I remember like before just doing like exactly that with the games is like while the left key is pressed, like move player moves like five pixels to the left. Like that's exactly.
So usually like in a game block, it's just. It's gonna be stuck in an infinite loop. But in like front end, back end development, you don't really have that kind of stuff. Okay. But the other way to write a while loop is like a do while loop. But that's like a very old way to write it. But I still use it this way sometimes. And it's like more semantically correct in my opinion. It's like do while this is true. So that's kind of like how I think about it. So it's just the same thing here. While condition block of code. It's just do block a code. And then while the condition.
Okay.
It's however you want to do it. It's just. All right, so now Number two, make an array of numbers and iterate over the array using a while loop.
Is my L there? L, E, N, G, th. I don't know why. There we go.
And now you forgot the parentheses. So it's very similar to the for loop. Right? The while loop, like if else. If else and switch are very similar for loop and while loop are very similar. It's just like different syntax, but they have their cases for both. All right, so cool. So you have a condition and you have something that's kind of like changing the condition. So that's always important to always, like, have something to change the condition or else you're going to get stuck in a while loop. Like, if we did this right, it's.
Just going to keep.
It's going to go forever. Right, I see.
It's going to keep logging the one every single time.
Yeah.
Or the zero space of the array.
Yeah. So you could do something like let condition equal, like through. Or like something like keep going while. And then you can say like while keep going. I'll keep going. Consol log. I'm going. And then you can say something like if here. Maybe you can say like I plus plus. And if I is. Wait, no, if I is over 10, let's do it until 9. We can say keep going equals false. Right. And that would be kind of another way to write it.
So it's looking at our I all the way here. Starts at zero. It's going to say. It's going to. While keep going is true. I'm going. And then finally, if I is less than 9, keep going is. Oh, we're going to change. Keep going is false once it goes to 10. And then that's what's going to stop our loop here.
Exactly.
Okay.
All right. And then it did run the two while loops. So it started I at. What was it? It was a five. Right. All right, cool. You understand the wild loop?
We'll find out. We'll find out when we go over. Kids, I'll do. I'll. I'll do the last homework, the one that you gave me, and then I'll see if I'll dabble in the wild loop stuff. And that if I. If I get the answer for this one. Yeah. We'll go from there. That. That.
Okay, that.
That homework is interesting.
I don't.
I know I gotta do math and I know I gotta add it up.
Yeah.
And then I gotta divide by the length of the. The length of the array.
Why don't you do a median, too? I think I have my notion. Open. Open.
What's median?
I think median is if you sort the numbers and the middle number.
More from Phil

day in the life of a software engineer | 5 hour productivity | building my dev agency 🔥

day in the life of a software engineer | 3 hour productivity | coding my 6-figure community startup

day in the life of a software engineer | 5 hour productivity | coding our sns startup app LFG

day in the life of a software engineer | 5 hour productivity | creating apps that makes big money






