Skip to 0 minutes and 11 secondsWe use loops to repeat tasks that we need to perform many times, either because it's tedious or because it's impractical to do to actually write all the different lines of code that needs to be performed. So if we are doing a task that is similar, we can actually just say let's do this line many times, and our life gets much easier. There are three different types of loops in Java, the while, the do while, the for loop. And they're all similar. They're basically the same. And then you're going to ask, why do we even have them? Well, because there are subtle differences that can make your life easier, depending on which one you choose.
Skip to 0 minutes and 57 secondsOr sometimes it's just up to the developer who likes it one specific way. But before you can start making choices about it, we need to look at how they work. And I have actually here in the DoDraw of our game added some code that shows all three in real life. For reasons even unknown to myself, I've decided that I would like to have the numbers from 1 to 9 drawn on the screen of our game while we are playing. I can't choose which loop I want to use, so I've written this little test, here. So the first three lines sets up what's called a paint. A paint is used when drawing things on screens.
Skip to 1 minute and 55 secondsAnd what we're basically saying here is that we want to write using a white colour and using a text font, which is 12 p, so quite small on some screens. You always change the size to something bigger. And then we have the code here with the for loops, not the for loops, the loops. I keep saying for loops, but the loops, the code here with the loops. And the first one is the while loop. And what we do here is that we first set up what we want to check. So i equals 0. This is going to be our variable, which will count from 1 to 9.
Skip to 2 minutes and 36 secondsAnd then in the first line of the while loop, we have a test, a condition, like an if statement. We're first checking if i is actually less than 9. And if it is less than 9, we'll go in and do the code, and run the code inside the block of the while loop. So of course in the beginning, we will, because it is less than 9. The first thing we do here in the block is to increase i with 1, so it would become 1, and then we print out. And here, we draw the text using the paint from above. And we write the while colon space and then 1 in the position of 10 F, which is the x.
Skip to 3 minutes and 20 secondsAnd the y will be 1 times 30, so 30. And that's where we will put this text. We continue the loop. We go up. And because i is now 1, it is still less than 9. And we will do the block again. And we'll keep on doing this until i is 9, because then, the condition will not be satisfied, and will then jump to the next line. Let's put a break point here, and let's debug the code and see what happens. So I'm using F8 to go through the lines. So now we have i, here. And you see it's 0, so therefore, we expect to go in. And we do.
Skip to 4 minutes and 9 secondsAnd we keep going, and we'll keep drawing until we get to becoming 9. So we see it's now 8. We're still going in. Now, it becomes 9. We draw the text. We do the test, and we go out.
Skip to 4 minutes and 26 secondsThat's it. That's a while loop. The next one we get to here is the do while loop. And it is basically the same. The only difference here is that the test will happen after we've done the block. So we will always go into the block at least once. That means that when I'm setting up the i, it should not be 0. It should be 1, because in that way, I will be able to just draw the text and increase it upwards. So let's go in F8, here. Again, I go in. I draw it, because now i is 1. It's fine. We will start there. It becomes 2, and because i is less than or equal to 9, we will continue.
Skip to 5 minutes and 10 secondsAnd we can do that until i is then 9. And we'll do a last one. Let's see. Now it's 9. We'll do a last one, because we're still satisfying it. We'll draw 9. Now it becomes 10. And we're not satisfying, so we go to the last line. Here, we have the for loop, the last of the loops. This is the one I'm using the most, because I find it's-- well, saves my typing. It's usually shorter than the others. In the for loop, we can basically do all the things we want to do with the other loops.
Skip to 5 minutes and 55 secondsThe first part of the for loop we're sitting up whatever we need to set up before we run the for loop. So here, I can say that i equals 1, Then we have the test that will be run. It's controlling it. It's like the while loop. We will test this first before going into the block. After that, once we have run the block, we will run what's at the end. So here, we will increase i with 1. The middle needs to be a condition. It needs to be a Boolean expression. It needs to be a test that is testing to true or false. But you can put whatever you like in the other. It just needs to be a statement.
Skip to 6 minutes and 45 secondsSo we can put whatever we like there. But remember, for this to finish, for this loop to finish, we would always at some point have to get to false. Otherwise, we'll just have a loop that never stops, which can be quite annoying, because that would usually be a bug. So here, if we run this, if we press F8, we'll see that we go in, because i is 1. Therefore, it is less than 9 or equals to 9. and we can draw the text. And we keep on doing this, and i goes on until we get to 9, or we go in, as well.
Skip to 7 minutes and 29 secondsBut 9 became 10 at the end, so it doesn't show the for loop, which is a little bit annoying when you're debugging. It doesn't show that it is going to go up before we go up to the for loop. So it looked like i was actually 9, but it becomes 10 before the test, And therefore, we jumped out. But now, we like to draw on everything. I'll just remove the break point and set it to continue running. So now we are just resuming to run. And if I go into the game, and I have it running in the background, you can see we have all the text we expect to be written here in the game.
Skip to 8 minutes and 14 secondsAnd if we're running it, you'll see that it's actually being drawn while we're playing it, because, well, I did it in the DoDraw. So this is continually being drawn while we are playing. It's not a good way of doing this. This is actually very inefficient, because at the moment, we are creating a lot of text all the time while we are running the game. That's actually very inefficient. Therefore, you won't find, for instance, the score being drawn in this way. It's drawn a little bit more efficient way in the background. But yeah, this is how we do for loops, and while loops, and do while loops.
Skip to 9 minutes and 0 secondsWe will soon in the next videos look at how we're going to use looping together with a race to create more obstacles, obstacles in front of the face in our game. But before we do that, I'll just remove this, because we don't need it. It was just to show you the point.
Introduction to loops
In life some tasks need to be repeated until the job is finished. The same is true in programming.
In this video, you’ll learn how ‘while loops’, ‘do-while’ loops and ‘for’ loops can be used to repeat statement(s) until a specified conditions are met. You’ll also see how loops are implemented in the game code, and the effects they have on gameplay.
Note: There is a downloadable guide available.
We will go through how to make these changes to the game step-by-step later in the week.