Transcript
00:00 Let's jump into for loops. So we're gonna start with our exhibit labels. We're making, exhibit labels, and we're basically building a numbered list. So we've got our string that we're going to accumulate, this list of values for. And then we need to create the initializer, the condition, and the final expression for our loop.
00:20 So we're gonna say four. And lots of times, the AI is going to be able to auto complete it for you. Your editor, might have a, thing here. So if I actually say four, you'll see we've got that and we can have it auto complete and then you've got all of this stuff going on. Just such a common, part of programming that, yeah, you're gonna get a lot of help with that.
00:38 And here, let's just look at these different pieces here. So four is the keyword telling the program, hey, I'm, like or the tokenizer, the thing that creates your program. I'm gonna be writing code for a for loop. We have the parenthesis around the contents of that for loop of of those three pieces. You have your initializer.
00:57 So this is setting up the variables that you need for your program. You can actually do multiple of these if you want. There's all kinds of interesting things you can do in here, but you don't typically see people doing that. Lots of times, this variable is gonna be called I, which is interpreted to mean the index. I try to avoid doing that when possible.
01:17 So we're gonna actually call this the exhibit number, to make it a little bit more clear what this is all about. And that is starting out as one. We're starting with our exhibit number of one. That's our label, here. And then as long as the exhibit number is less than or equal to five, the loop will continue.
01:35 So this is what we call the condition. And then our final expression is what do we do at the end of the loop and exhibit number plus plus? That's basically a special syntax for numbers that says take the value of that number currently, add one to it, and reassign the variable to that. So it's the same sort of thing as saying goodness, plus equals that. Those are or plus equals one.
02:01 There we go. And that equals that. That's basically the same sort of idea there. So that's what's going on with that plus plus thing. So at the end of each iteration, we increment that and, once that value gets above five, then we'll break out of this for loop.
02:19 And that's one thing you have to be really careful of is, both this condition Well, in fact, all three of these are a really important part of what enables you to break out of a for loop. Similar to a switch actually, you can put a break anywhere inside of any type of loop and that will immediately break out of the loop as well. So you could have a condition right here and put the break inside of there and that will immediately switch out. Another thing we're not really going too deep into but we can say, continue right here. And that basically says, hey, skip exhibit number three.
02:55 I'll leave that in there so we can explore that a little bit more. So let's, do this and you'll see we have exhibit one, two, and we skip three, four, and five. So that's what's going on here. You could also say if exhibit number is not equal to three, then, we get the same results here. But sometimes you've got a lot of code that's going on down here and it's just easier to do what we call an early exit, where we just say, skip this one, continue, and, and it will continue on to evaluate that final expression and and then iterate.
03:26 Another thing that I wanna mention here when we're talking about early exits, something that I do a lot is I will put those early exits on a single line. If it fits on a single line, then that works. You can technically put braces on there, but your formatter is typically gonna reformat it, in that way. So, I like to do a single line like this and that works pretty well. Let's get rid of that.
03:51 We've got our exhibits one, two, three, four, five and that is all working swimmingly. I think I threw a whole bunch of stuff at you. I recommend that you actually play around with some of the things that I just showed you in this, in this video and, see to make sure that you understand how that stuff works. Like that early exit thing works just as well with break, and when we get to functions, it works with returns as well. And we've got these, yeah, the the block with the early or the final expression and the condition and the initializer.
04:28 All of this stuff's kinda interesting and I recommend that you just kinda play around with it a little bit. So there you go. That is your first introduction to loops. Great job.
