In this article, we'll look at a core aspect of the Java language – executing a statement or a group of statements repeatedly – using loops.
2. Intro to Loops
In programming languages, looping is a feature which facilitates the execution of a set of instructions until the controlling Boolean-expression evaluates to false.
Java provides different types of loops to fit any programming need. Each loop has its own purpose and a suitable use case to serve.
Here are the types of loops that we can find in Java:
- Simple for loop
- Enhanced for-each loop
- While loop
- Do-While loop
3. For Loop
A for loop is a control structure that allows us to repeat certain operations by incrementing and evaluating a loop counter.
For a detailed example, have a look at the dedicated post: Java For Loop.
4. While Loop
The while loop is Java's most fundamental loop statement. It repeats a statement or a block of statements while its controlling Boolean-expression is true.
For a detailed example, have a look at the dedicated post: Java While Loop.
5. Do-While Loop
The do-while loop works just like the while loop except for the fact that the first condition evaluation happens after the first iteration of the loop.
For a detailed example, have a look at the dedicated post: Java Do-While Loop.
In this quick tutorial, we showed the different types of loops that are available in the Java programming language.
We also saw how each loop serves a particular purpose given a suitable use case. We discussed the circumstances that are suitable for a given loop implementation.
As always, examples can be found over on GitHub.