Java For Loop W3s Tutorial Guide Examples

Robert Jul 13, 2026

The Java for loop is a fundamental construct used to repeat a section of code multiple times. W3Schools, a renowned online learning platform, provides comprehensive information about Java for loops. Let's delve into the details of Java for loops and various use cases.

a poster with different types of text and symbols on the back ground, including an image of
a poster with different types of text and symbols on the back ground, including an image of

For loops are essential when you need to iterate over a range of values or elements in an array. They consist of three components: initialization, condition, and iteration. By understanding and mastering for loops, you can enhance your Java programming skills significantly.

the quick and easy way to learn how to use loops
the quick and easy way to learn how to use loops

Understanding the Basic Syntax

The basic syntax of a Java for loop is as follows:

Java for Loop (With Examples)
Java for Loop (With Examples)

for (initialization; condition; iteration) {
    // code to be executed
}

The initialization part is executed once when the loop begins. The condition, checked before each iteration, determines if the loop continues. If true, the code block is executed. The iteration section is run at the end of each iteration.

Initialization, Condition, and Iteration

Loops In Java – Best And Complete Guide For Beginners
Loops In Java – Best And Complete Guide For Beginners

Here's an example illustrating each part of the for loop:

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

In this loop:

  • Initialization: int i = 0;
  • Condition: i < 10
  • Iteration: i++ (increment i by 1)
an image of a computer screen with text that reads 1 for loop used when the number of
an image of a computer screen with text that reads 1 for loop used when the number of

Infinite Loops and the break Statement

An infinite loop occurs when the condition never becomes false. To create an infinite loop, specify a condition that will always evaluate to true:

for (;;) {}

To exit an infinite loop or any loop prematurely, use the break statement:

Coding with Parvez
Coding with Parvez

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}

Looping Through Arrays and List

For loops can iterate over elements in arrays and lists, making them invaluable for data processing:

🟠 UNDERSTANDING JAVA LOOPS πŸ”„
🟠 UNDERSTANDING JAVA LOOPS πŸ”„
Loops in Java
Loops in Java
While Loop in Java Explained | Java Programming Basics for Beginners
While Loop in Java Explained | Java Programming Basics for Beginners
What are loop control statements in Java?
What are loop control statements in Java?
For Loop in Java Explained | Java Programming Basics for Beginners
For Loop in Java Explained | Java Programming Basics for Beginners
Java Loops - An Ultimate Guide to Master the Concept
Java Loops - An Ultimate Guide to Master the Concept
FOR LOOP FIND FOR Any Letter in JavaScript
FOR LOOP FIND FOR Any Letter in JavaScript
πŸ”΄πŸ”¦ Master Java Loops in Minutes! πŸš€πŸ”¦
πŸ”΄πŸ”¦ Master Java Loops in Minutes! πŸš€πŸ”¦
The OOP'S Concept in JAVAπŸ€”πŸ’―βœ…πŸ‘‡
The OOP'S Concept in JAVAπŸ€”πŸ’―βœ…πŸ‘‡
Java Learning Roadmap 2026 β˜•πŸš€
Java Learning Roadmap 2026 β˜•πŸš€
two different types of loop for white and black numbers on yellow background with text below
two different types of loop for white and black numbers on yellow background with text below
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 12
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 12
How to Become a Java Developer (Step-by-Step)
How to Become a Java Developer (Step-by-Step)
Do-While Loop in Java Explained | Java Loop Concepts for Beginners
Do-While Loop in Java Explained | Java Loop Concepts for Beginners
While Loop in Java with Example - Scientech Easy
While Loop in Java with Example - Scientech Easy
Cycle Completed! Prime Python, Java, JS Services: Loop Closed – Order for Infinite Innovations!
Cycle Completed! Prime Python, Java, JS Services: Loop Closed – Order for Infinite Innovations!
Follow @CodeWithAdya for more Python notes, coding tips, and beginner-friendly content πŸ’»πŸ’œ #Python
Follow @CodeWithAdya for more Python notes, coding tips, and beginner-friendly content πŸ’»πŸ’œ #Python
javscript runtime
javscript runtime
continue in While loop in java
continue in While loop in java

Looping Through Arrays

Here's an example of a for loop iterating through an array:

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

Looping Through Lists

In Java 8 and later, use the enhanced for-each loop to simplify iterating through lists:

List<String> list = Arrays.asList("apple", "banana", "cherry");
for (String fruit : list) {
    System.out.println(fruit);
}

Understanding Java for loops and their applications in various contexts can significantly improve your coding efficiency. So, keep practicing and exploring new use cases to expand your skills.