Often times as a programmer, you’ll encounter situations where you’ll need to use loops – it’s inevitable! So learning how to use loops in Java as fast as possible is a good idea!
Why use loops?
Well, this is pretty self-explanatory. If you want a specific set of instructions to execute for a specific number of times or for all of eternity, you’ve got the reason right there! You use loops in java when you want to repeat a set of instructions. Java has three different loops. Those are:
- For loop
- While loop
- Do…While loop
In java 5, the enhanced for loop was introduced.
Let’s get on with when to use the four loops!
Let’s start with the For Loop!
The For Loop
The for loop is used if you want to execute a set of instructions for a set number of times. When you want to use a for loop, you need to declare an integer variable. You then create a boolean condition. You basically specify when to stop the loop. The loop will stop once the variable you declare holds a value either equal or greater than a value you specify. Then, after the loop has ran its course once, the integer variable will increment or decrement by whatever you specify.
This might be confusing, but I’ll show you an example of how it works!
[crayon-5bf0504b7f182338464784/] The first thing we did was declare an integer variable called i. Afterwards, we set the condition. We tell the computer that our integer variable is less than 10. So the loop will run until our variable isn’t less than 10 anymore. Right after we set the condition, we specify what is going to happen after the loop is done with the instructions. In this case, we specified what we want it to increment our variable by one. Between the curly braces we set the instructions we want the loop to carry out. For simplicity’s sake, we’re just going to make it print out the value of our variable. What do you think will happen? Can you guess the output?
This is the output:
[crayon-5bf0504b7f18d321078584/] Of course, we can make it look nicer but that’s not the point of this tutorial. You’ll be using for loops in java a lot.
The while loop
Rest assured, this loop is easier to grasp than the previous one. This one is pretty simple. While a condition is true, the loop will execute the specificed instructions. The while loop can actually be used the same way you use the for loop, although that would be quite inefficient – and quite silly, if I dare say.
Here’s an example of the usage of the while loop:
[crayon-5bf0504b7f192592861660/] While loops are commonplace in game systems. Ever heard of a game loop before?
The do…while loop
This loop functions similarly to the while loop with only one difference. This loop will execute at least once. If I try to use a while loop and the condition was false to begin with, the loop wouldn’t execute at all. So, if we want to be certain that it will execute at least once, we can use the do…while loop.
Here’s an example of the code for the do…while loop:
[crayon-5bf0504b7f198277122344/] The code does pretty much what a for loop can do but it’s just to show you how it works. Notice how the while part is at the end instead of in the start.
The enhanced for loop
This loop is used when you want to loop through an array. With this loop, you assign create a temporary variable which will go through the array and hold the value of each and every element in the array. This gives you the ability to manipulate every element in the array.
Here’s an example of the code:
[crayon-5bf0504b7f19c315030460/] What this does is declare a temporary variable then assign the variable with the first element in the array. Then the loop executes the instruction which in our case is just a simple print command. Then when the loop has executed all of the instructions, our temporary variable is assigned the value of the second element in the array. And it continues like this until there aren’t any more elements left in the array.
Now, there are ways for you to break out of loops without letting the loop finish looping. Yeah, anyway…
You can accomplish this by using the…
The break keyword stops the loop completely. Here’s an example of its usage:
[crayon-5bf0504b7f1a1114219688/] The output would look like this:
[crayon-5bf0504b7f1a6891574261/] That’s right, the loop would terminate before it got to the print command.
But what if you want to skip an element in the array? You could use…
The Continue Keyword
Here’s an example of how to use the continue keyword:
[crayon-5bf0504b7f1aa094009035/] This code snippet would produce the following output:
Having to use any of these loops is an inevitable event and I highly suggest creating a few programs yourself to get used to using loops.