Java W3schools For Loop Tutorial Guide Examples

Robert Jul 13, 2026

The humble 'for' loop is a cornerstone of programming, enabling us to repeat a block of code a specific number of times. If you're new to Java or simply need a refresher, you've come to the right place. This guide, drawing from the extensive resources of W3Schools, will walk you through the intricacies of Java 'for' loops, helping you master this fundamental concept.

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

Java 'for' loops are sister to their C-like counterparts, sharing the same basic syntax. By inserting a loop counter, you specify exactly how many times the loop should iterate, making 'for' loops particularly useful when you know in advance how many times your code needs to run.

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

Understanding Syntax

Before diving into examples, let's break down the syntax of a basic Java 'for' loop:

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

```java for (initialization; condition; increment/decrement) { // code to repeat } ```

The initialization part runs once before entering the loop. The condition is checked before each iteration – if it's true, the loop continues; if false, it stops. The increment/decrement operation occurs after each iteration.

Let's look at the structure in action with some key examples.

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

Basic For Loop

The simplest form of the 'for' loop looks like this:

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

Here, the loop starts at 0 (initialization), continues while i is less than 5 (condition), and increments i after each loop (increment). The output is:

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

``` 0 1 2 3 4 ```

Looping Through an Array

Often, you'll use a 'for' loop to traverse an array or list. Here's how you'd loop through an array:

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

The output is the same as the previous example, but this time we used an array.

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

Incremental and Decremental Loops

So far, we've only seen loops that increment the counter. But what if you need to count down? Or increment by a value other than 1?

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
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
Java Learning Roadmap 2026 ☕🚀
Java Learning Roadmap 2026 ☕🚀
🟠 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
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
continue in While loop in java
continue in While loop in java
🔴🔦 Master Java Loops in Minutes! 🚀🔦
🔴🔦 Master Java Loops in Minutes! 🚀🔦
W3Schools.com
W3Schools.com
Beginner's Guide to Learning Java
Beginner's Guide to Learning Java
How to Become a Java Developer (Step-by-Step)
How to Become a Java Developer (Step-by-Step)
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
What are loop control statements in Java?
What are loop control statements in Java?
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
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
Do-While Loop in Java Explained | Java Loop Concepts for Beginners
Do-While Loop in Java Explained | Java Loop Concepts for Beginners

Here's how to do both:

Decrementing Counter

Changing the increment part to `i--;` makes the loop count down:

```java for (int i = 5; i > 0; i--) { System.out.println(i); } ```

The output is:

``` 5 4 3 2 1 ```

Custom Increment

If you want to increment by a value other than 1, simply specify it. Here's an example that increments by 2:

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

The output is:

``` 0 2 4 6 8 ```

Advanced For Loops

In some cases, you might not need to manually control the loop counter. This is where advanced for loops come in handy.

Java provides two types of enhanced for loops, also known as foreach loops. They allow you to loop through an array or a list without needing to manually initialize and increment a counter.

Array For Each Loop

The first type of enhanced for loop allows you to iterate through an array:

```java int[] numbers = {1, 2, 3, 4, 5}; for (int number : numbers) { System.out.println(number); } ```

The output is the same as all the previous examples. Because it's a foreach loop, you don't need to use an explicit index.

String For Each Loop

You can also use an enhanced for loop to iterate through each character in a string:

```java String str = "Hello, World!"; for (char c : str.toCharArray()) { System.out.println(c); } ```

The output is:

``` H e l l o , W o r l d ! ```

In both enhanced for loops, the colon (`:`) separates the variable from the iterable object.

To conclude, Java's 'for' loops offer a robust and versatile way to repeat code. From basic counter-based loops to elegant foreach loops, these constructs are invaluable in numerous coding situations. Keep practicing and honing your skills with 'for' loops – they'll serve you well on your programming journey!