Looping is a fundamental and essential feature in every Programming Language including Java. Loops a control flow statement for traversing or iterating items in a collection, array, list, set, etc. There are a lot of ways to loop through collection, array, list, or set in Java. It can use classic for, do-while, while, or for-in. This time, we will show you examples that are used in real Java applications or projects.
Table of Contents:
- For Loop Examples
- For In-Loop Examples
- Infinite Loop Examples
- While Loop Examples
- Do/While Loop Examples
- Nested Loop Examples
Before jumping to the example, make sure you have installed Java Virtual Machine and the Java Development Kit (JDK). You can download and install it from Oracle https://www.oracle.com/java/technologies/downloads/ or OpenJDK https://openjdk.org/projects/jdk/19/. Then set the JAVA_HOME or java and javac commands to the environment variable. After Java setup and can run correctly then open your IDE or Text Editor. We are using VSCode, so, in the Java apps folder we just type this command.
cd JavaApps
code .
To check the installed java version, type these commands in the terminal or cmd.
java -version
java version "19.0.2" 2023-01-17
Java(TM) SE Runtime Environment (build 19.0.2+7-44)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)
javac -version
javac 19.0.2
Now, in the VSCode, create a new Java file JavaLoopsExample.java. We will put all examples for this tutorial in this file.
For Loop Examples
The first example will be running inside the main method that will execute first. For that add these lines of codes in the file JavaLoopsExample.java.
public class JavaLoopsExample {
public static void main(String[] args) {
}
}
The classical looping thought the collection, array, list, or set in Java or C is the "for" keyword.
Classic For Loop example:
String[] animals = { "Lion", "Elephant", "Fox", "Eagle" };
for (int i = 0; i < animals.length; i++) {
System.out.println(animals[i]);
}
Results:
Lion
Elephant
Fox
Eagle
For loop with comma-separated expressions example:
for (int i = 1, j = 10; i < j; i++, j--) {
System.out.println(i + ", " + j);
}
Results:
1, 10
2, 9
3, 8
4, 7
5, 6
For loop multi variables example:
for (int x = 0, y = 10, z = 1; z < y; z++)
System.out.println("X: " + x + ",Y: "+ y + ", Z: " + z);
Results:
X: 0,Y: 10, Z: 2
X: 0,Y: 10, Z: 3
X: 0,Y: 10, Z: 4
X: 0,Y: 10, Z: 5
X: 0,Y: 10, Z: 6
X: 0,Y: 10, Z: 7
X: 0,Y: 10, Z: 8
X: 0,Y: 10, Z: 9
The multiple variables in this example should be the same type.
For In-Loop Examples
Iterate over a list of objects example:
import java.util.ArrayList;
import java.util.List;
class JavaLoopsExample {
static class Framework {
String name;
String type;
int gitStars;
}
public static void main(String[] args) {
List<Framework> frameworks = new ArrayList<>();
Framework framework1 = new Framework();
framework1.name = "Spring";
framework1.type = "Java";
framework1.gitStars = 32000;
frameworks.add(framework1);
Framework framework2 = new Framework();
framework2.name = "Flask";
framework2.type = "Python";
framework2.gitStars = 10000;
frameworks.add(framework2);
Framework framework3 = new Framework();
framework3.name = "Quarkus";
framework3.type = "Java";
framework3.gitStars = 50000;
frameworks.add(framework3);
for (Framework framework: frameworks) {
System.out.println(framework.name + " (" + framework.type + ") -> " + framework.gitStars);
}
}
}
Results:
Spring (Java) -> 32000
Flask (Python) -> 10000
Quarkus (Java) -> 50000
Iterate over an array of string examples:
String[] carNameArray = { "BMW", "Mercedez Benz", "Volvo" };
for (String car: carNameArray) {
System.out.println(car);
}
Results:
BMW
Mercedez Benz
Volvo
Iterate over map example:
import java.util.HashMap;
import java.util.Map;
class JavaLoopsExample {
public static void main(String[] args) {
Map<String, Integer> cars = new HashMap<>();
cars.put("BMW", 45000);
cars.put("Mercedez Benz", 44000);
cars.put("Volvo", 30000);
for (Map.Entry<String, Integer> car :
cars.entrySet()) {
System.out.print(car.getKey() + ":");
System.out.println(car.getValue());
}
}
}
Results:
Volvo:30000
Mercedez Benz:44000
BMW:45000
Infinite Loop Examples
The basic loop without any variable and assignment will be an infinite loop.
Infinite loop example using for:
import java.util.Date;
class JavaLoopsExample {
public static void main(String[] args) {
for(;;) {
System.out.println(new Date());
}
}
}
Results:
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
...
Press CTRL+C to stop the infinite loop.
Infinite loop example using do-while:
do {
System.out.println(new Date());
} while (true);
Results:
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
...
Press CTRL+C to stop the infinite loop.
Infinite loop example using while:
while (true) {
System.out.println(new Date());
}
Results:
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
Tue Mar 14 16:00:14 WIB 2023
...
While Loop Examples
Iterate over an array of strings using classic while example:
String[] cars = { "BMW", "Mercedez Benz", "Volvo" };
int x = 0;
while ( x < cars.length ) {
System.out.println(cars[x]);
x++;
}
Result:
BMW
Mercedez Benz
Volvo
Do/While Loop Examples
Classic Java-style do/while example:
String[] cars = { "BMW", "Mercedez Benz", "Volvo" };
int x = 0;
do {
System.out.println(cars[x]);
x++;
} while(cars.length > x);
Results:
BMW
Mercedez Benz
Volvo
Nested Loop Examples
A nested loop is a loop inside a loop or a loop has a child loop.
Nested loop example to print simple pyramid:
int x, y;
// parent loop
for(x = 0; x < 7; x++) {
// child loop
for(y = 0; y <= x; y++)
{
System.out.print("* ");
}
System.out.println();
}
Results:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Nested loop example to print triangle:
for (int x = 0; x < 7; x++) {
for (int y = 7 - x; y > 1; y--)
{
System.out.print(" ");
}
for (int y = 0; y <= x; y++ )
{
System.out.print("* ");
}
System.out.println();
}
Results:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
That it's, an example of Java loops. We are wrapping the examples in one file in GitHub.
That just the basic. If you need more deep learning about Java and Spring Framework you can take the following cheap course:
- Java basics, Java in Use //Complete course for beginners
- Java Programming: Master Basic Java Concepts
- Master Java Web Services and REST API with Spring Boot
- JDBC Servlets and JSP - Java Web Development Fundamentals
- The Complete Java Web Development Course
- Spring MVC For Beginners: Build Java Web App in 25 Steps
- Practical RESTful Web Services with Java EE 8 (JAX-RS 2.1)
Thanks!