The for loop and the while loop have been programming language staples for a long time now, as they have existed is many, many languages.
If you've ever played Tetris, you know that the game never truly ends. Blocks continue to fall one at a time, increasing in speed as you go up in levels, until the game breaks from bad coding or the player loses. How many lines of code would it take to create a game that goes on forever!? I mean, can you imagine copying hundreds of lines of code over and over and over again until it seems like the game will take a sufficiently long time? There's definitely got to be a way to avoid rewriting the same code over and over again right?
Lucky for us, there is a way to get our code to repeat, and that's by using loops. Java has two main ways of looping, and those are the "for loop" and the "while loop".
For starters, let's look at how to set up a for loop by first demonstrating what one looks like.
This is a good time to talk about the ++ operator. Let's look at a simple integer variable:
Here m is equal to 5. Now I'm going to add this line of code under it:
This adds one to m without having to do m = m + 1; It has some technical rules that we won't deal with in this tutorial, but just know that it adds one to the m variable. In this example m is equal to 5, so this new line makes m equal to 6. Likewise m--; subtracts one. There's no equivalent for multiplication or division.
So back to the loop itself. We start off with the "for" keyword. Notice how it is in lower case. Next the terms of the loop go inside the parenthesis. We'll take this one step at a time.
The first thing you see is int x = 0; This is the starting point of the loop. It says that at the beginning of the loop, an integer variable named x is going to be zero. Next you will see x < 5; This is saying that the loop should continue so long as x is less than 5. The last part says x++. This means that every time the loop repeats, it will add one to x. So, to recap...
The first part is the starting position of a variable that counts the number of loops. The second part tells the for loop how many times to loop. The third part tells the for loop how to count. In our case we're counting up by one, but that does not have to be the case. You could do:
That will add 2 to x every time the loop starts over, however I recommend for now just using x++.
The last part of the for loop is the curly brackets. Every piece of code that you want repeated has to go inside of those brackets.
Let's say I wanted my program to print out Hello world! 5 times. This is how I would use a for loop to do it:
This will output Hello world! on the screen five times. Now let's do something more impressive. We're going to count to ten.
Yes, you can print out the counter variable amongst other things. You can do anything you want with the variable just as you can any other variable. Here I started the count at 1 instead of 0 because I wanted to count from 1 to 10, and not 0 to 9. You'll also notice I needed to change the conditional part of the for loop so that x stops after 10. Give this example a try and see for yourself how it works. Try to experiment with different starting conditions etc.
The second basic type of loop in Java that I will discuss is the "while loop". A while loop is actually just a conditional that repeats itself as long as the condition stays true. It looks a lot like an if statement. Here take a look:
A while loop looks just like an if statement; just replace the "if" keyword with the keyword "while". The statement above says that while num is equal to 3, keep executing whatever code is inside the brackets. When the bottom of the code in the brackets is reached, it goes back up to the top, checks to see if num is still equal to 3, and then repeats again.
It is up to you the coder to insure that this loop eventually has a way to get out. Java will NOT figure this out for you. So pay attention to your condition unless you want to be stuck in an infinite loop.
An infinite loop example:
You now have the tools at your disposal to make cool little text based programs with Java. Congratulations! If you have any questions or need additional help, again you may use the form below to get in touch with me, and I'll be glad to help.
There's actually one more beginner's tutorial after this, so if you want to truly press on and get all the background you need to start learning the more advanced Java, then I recommend moving on with the final beginner's tutorial!
If you have any questions, comments, or concerns about this tutorial, feel free to contact us.