6.3: For Loop – Processing Tutorial

Okay in this video. We are going to be talking about a for loop. Here’s the thing… You learned about a while loop, you can actually just totally skip this video. You don’t need a for loop. A for loop is just a shorthand way of writing a while loop. A certain kind of while loop that you tend to want to write over and over and over and over again. But you’re going to be looking at Processing examples You’re going to be doing things in the future that for loop is going to be convenient for you. So you might as well get exposed to it and use it but I should really point out that we’re not adding any new functionality, we’re just learning a shorthand way to write a while loop. In fact the way we’re going to write it is perhaps a little bit more confusing than a while loop. but
and we’re going to examine that. So that’s all this video is going to be about it’s about let’s look at the while loop. Let’s look at a for loop. Let’s learn how to rewrite a while loop as a for loop and let’s do that in Processing code. So we have the answer to one of our exercises earlier. I refactored this program just to make it a static program so there’s no setup() and draw() anymore and all it’s doing is it’s saying: “Hey let’s start x at zero and repeat x all the way to the edge of the screen. Let’s start y at zero let’s repeat y all the way to the bottom of the screen? That’s what we’re doing in this video to draw this sort of grid pattern by lines horizontally and lines vertically. So to do that. We know we have to say okay well first we start with x equal to zero. Then we say well while x is less than the width of the window. Draw something and increment… So draw something being the line. Sorry. The line. Draw the line which goes from X comma 0 to x comma height and then increment x by some amount I think I said 20 pixels an arbitrary choice. This is our while loop and gives us a line, a line, a line, a line all the way to the edge. So… What are the elements of this loop? There are three elements one we could say is initialization condition? This is where we set up, what is the thing. We’re initializing it right before our loop starts. x equals zero. What is another thing we’re doing? We have a boolean expression. We need to check if that boolean expression is true. If it is continue looping if it’s false finish looping. What’s another thing we have? We have an incrementation operation. Incrementation operation. What we’re doing is we’re incrementing some variable every time at the end of the loop. Ensuring that our exit condition is eventually met. Well a for loop takes these three common elements Something that you initialize, a boolean expression that you test, and something that you do at the end of the loop and allows you to write them all in one statement. So it looks like this for int x equals zero, semicolon x is less than width, semicolon, x equals x plus 20, end parentheses, curly bracket, close curly bracket. Now I have to walk up to the camera here for a second. Yes, that curly bracket is in the shot. You are seeing it that’s wonderful. I could also come up and say: “Helllo.” Okay Somebody told me to do that. And so I did it. Now I feel embarrassed and also kind of stupid. Okay, we’ll edit that out. So here we are, look at this we have element number one is right here. Element number two is right here. Element number three is right here What’s element number four well element number four is the stuff that you do inside the loop which for us is: draw a line. So you can see these two loops are 100% identical The difference is… you know…. This allows you to stick these three lines of code next to each other on one line. It makes your code a little shorter. You’re just saying hey the loop starts with x at zero We always check if x is less than width. At the end of loop we always go up by 20. You know in some world, which is your world if this is the first time you’re learning is… This this is actually quite a bit more confusing Because this really says you know this is all happening in the order first do this then check the expression then draw the thing then increment. Check the expression then draw the thing then increment. Check the expression. Oh the expression is false leave the loop. Here I…. Hear we’re going out of order. First we do this, then we do this, then we do this, then we do this, then we do this again, and if that’s true… right so it’s a little bit out of order. But it will become intuitive to you but I would suggest as an exercise what you should be doing is find something you made that has a while loop in it try to rewrite it with a for loop get the exact same result. In fact you could do that if you want to pause right now you could do that with this grid example put this in your code run it. Oh wait why would you do that. I already did it for you, but let’s just go… you can pick something else. I’ll think of something I’ll try to think of something. Let me.. ah what time… we’re only at five minutes! This is a brilliant video. This… I’m just going to go let’s go put this in and prove that this works. So here we have a loop for x and a loop for y which is convenient because we can see it as both a while loop. But what I’m going to do now just for the purpose of demonstration you probably wouldn’t do this in your code because it would be confusing
I’m going to have one the x stays as a while. The while… the y change to a for loop and we will look at it that way. Okay, so we know that the for loop… The first thing we do is we need the increment… the the initialization condition, okay… then the boolean expression is y less than height, then the incrementation operation and then what do we do inside that loop as we draw a line. So you can see… these loops are identical principally identical like conceptually… principally… conceptually identical. They’re doing the same thing. One for horizontal values one for vertical values. But you can see how this is condensed. It reads got like three lines of code This is one, two, three, four, five. You know if you’re going to do this kind of loop over and over again It’s nice to have the shorthand. So let’s run this make sure we get the same result. We got the same result you know if you want a really simple… What’s the easiest exercise I could give you take this and just convert this first one to a for loop. You know, actually it’s not that easy. This stuff is weird. Like look at the semicolons, parentheses, with what order? So this will take some time for you getting used to but hopefully this gives you a sense of what we understand what a while loop does and what does a for what is a for loop? It’s the same thing as a while loop but it is a shorthand. The while loop by the way is going to be a great deal more flex. You could have multiple lines of code here. You could be doing all sorts of weird stuff in here. This is a very specific format, which gives you three options. Okay. That is perhaps the shortest video. I think someone’s laughing in the hallway. I swear, that they heard me talking to myself, and they’re laughing now. Look they’re laughing with me. This is very funny for all of us. Anyway, okay… This is the end of the video there will be another one. See you soon.

Add a Comment

Your email address will not be published. Required fields are marked *