W3Schools Java For Loop Mastery Guide Complete Tutorial

Robert Jul 13, 2026

Whether you're a seasoned coder or just starting your programming journey, understanding loops is crucial. In Java, "for" loops are an integral part of controlling the flow of your code. W3schools provides an excellent resource to grasp this concept, so let's dive into the Java "for" loop, using the tutorials and examples from W3schools as our guide.

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

The Java "for" loop is used when you know how many iterations the loop should perform. Unlike "while" or "do while" loops, it initializes, tests, and increments the loop counter (i) in one concise statement. Let's explore its syntax and functionality with some hands-on examples from W3schools.

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

Understanding the Syntax

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

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

for (initialization; condition; increment/decrement) { statement; }

The initialization statement runs first, setting the initial value of the loop counter. The condition determines if the loop continues or stops. The increment/decrement statement alters the loop counter's value. Once the loop condition is false, the loop finishes, and the program continues with the next statement.

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

Initialization, Condition, and Increment/Decrement

W3schools explains each part clearly. Initialization sets the starting point, say, int i = 0;. Condition sets the loop's duration, like i < 5;. Increment/decrement changes the loop counter's value, e.g., i++.

Let's see an example where we print numbers from 1 to 5:

Java While Loop Explained 🔁 | Easy Guide for Beginners
Java While Loop Explained 🔁 | Easy Guide for Beginners

for (int i = 1; i < 6; i++) { System.out.println(i); }

Java "for" Loop with Index

Java "for" loops are often used with an index variable, which increments or decrements with each iteration. Here's an example, also found on W3schools, where we print even numbers between 2 and 8:

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

for (int i = 2; i <= 8; i+=2) { System.out.println(i); }

In this case, the loop counter "i" starts at 2 and increments by 2 in each iteration until it reaches 8.

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
W3Schools.com
W3Schools.com
Coding with Parvez
Coding with Parvez
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
🟠 UNDERSTANDING JAVA LOOPS 🔄
🟠 UNDERSTANDING JAVA LOOPS 🔄
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)
What are loop control statements in Java?
What are loop control statements in Java?
continue in While loop in java
continue in While loop in java
Java Learning Roadmap 2026 ☕🚀
Java Learning Roadmap 2026 ☕🚀
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Do-While Loop in Java Explained | Java Loop Concepts for Beginners
Do-While Loop in Java Explained | Java Loop Concepts for Beginners
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
FOR LOOP FIND FOR Any Letter in JavaScript
FOR LOOP FIND FOR Any Letter in JavaScript
an image of a flow diagram with two different types of loops and loops on it
an image of a flow diagram with two different types of loops and loops on it
Beginner's Guide to Learning Java
Beginner's Guide to Learning Java
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners

Nesting "for" Loops

Java "for" loops can also be nested within other loops to create complex control flow. Nested loops often come in handy when you need to iterate over multiple dimensions, like in a 2D array or a grid. Let's consider a simple example from W3schools, printing a multiplication table:

123
246
369

Here's how we can achieve this using nested "for" loops:

for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { System.out.print(i * j + " "); } System.out.println(); }

Each "for" loop manages one dimension (row or column), and the output is printed line by line.

Infinite "for" Loops

It's also possible to create an infinite "for" loop, but be cautious – these loops can cause infinite program execution if not properly controlled. An example, again from W3schools, is:

for (;;) { System.out.println("Infinite Loop"); break; }

The "break" statement is necessary to halt the loop after one iteration.

W3schools offers numerous examples and an interactive console to experiment with these loops. Learning and practicing with these resources can greatly enhance your understanding of Java "for" loops. So, dive into the tutorials, and happy coding!