Skip main navigation

Introduction to loops

Find out how loops can be used to repeat tasks in the programme until certain conditions are met.
We 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.
Or 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.
And 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.
And 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.
And 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.
And 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.
That’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.
And 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.
The 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.
So 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.
But 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.
And 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.
We 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.
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.
There is a downloadable guide available at the bottom of this Step, should you wish to keep it to hand while going through the video.
We will go through how to make these changes to the game step-by-step later in the week.
This article is from the free online

Begin Programming: Build Your First Mobile Game

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education