- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
The for loop in Java is used to execute a block of code repeatedly for a specified number of times. It consists of three parts: initialization, condition, and update.
Example
for (int i = 0; i < 5; i++) {System.out.println(i);}Copied!✕CopyIn this example, the loop starts with i initialized to 0, runs as long as i is less than 5, and increments i by 1 after each iteration.
Nested For Loop
A nested for loop is a for loop inside another for loop. This is useful when you need to perform more complex iterations.
Example
for (int i = 1; i <= 3; i++) {for (int j = 1; j <= 3; j++) {System.out.println("i: " + i + ", j: " + j);}}Copied!✕CopyIn this example, the outer loop runs three times, and for each iteration of the outer loop, the inner loop also runs three times.
Important Considerations
Infinite Loop: Be cautious of creating infinite loops by ensuring the condition will eventually become false.
Performance: Nested loops can be computationally expensive, so use them judiciously.
Java For Loop - W3Schools
Java For Loop When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
See results only from w3schools.comTry It Yourself
The W3Schools online code editor allows you to edit code and view the result in …
Java User Input
Java User Input The Scanner class is used to get user input, and it is found in the …
Java Data Types
Note: This rule makes Java safer, because the compiler will stop you if you try to mix …
W3Schools Tryit Editor
The W3Schools online code editor allows you to edit code and view the result in …
Real-Life Examples
Real-Life Examples - Java For Loop - W3Schools
W3schools Exercise
Show AnswerHide Answer Submit Answer » What is an Exercise? To try more JAVA …
Java For Loop - GeeksforGeeks
2 days ago · Examples and Usage of for loop The following examples demonstrate how for loops and nested for loops are used in Java for iteration, pattern printing, and calculations.
- People also ask
Java for Loop (With Examples) - Programiz
In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming.
Java For Loop - Baeldung
Feb 16, 2025 · A for loop is a control structure that allows us to repeat certain operations by incrementing and evaluating a loop counter. Before the first iteration, the loop counter gets initialized, …
The for Statement (The Java™ Tutorials > Learning the Java ...
Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:
Java For Loop - DataCamp
Learn how to use the Java for loop to iterate over arrays and collections efficiently. Discover syntax, examples, and best practices for optimizing your code.
Mastering the Java `for` Loop: Concepts, Usage, and Best ...
Nov 12, 2025 · This blog post will delve into the fundamental concepts of the Java for loop, explore different usage methods, discuss common practices, and present best practices to help you become …
Java - for Loop - Online Tutorials Library
Below are various examples demonstrating the usage of the for loop in Java: In this example, we're showing the use of a for loop to print numbers starting from 10 to 19.
Java For Loop (with Examples) - HowToDoInJava
Nov 20, 2023 · The for-loop statement in Java provides a compact way to iterate over the arrays or collection types using a counter variable that is incremented or decremented after each iteration.
Understanding For Loop in Java With Examples and Syntax
Jul 31, 2025 · Java provides three types of loops, i.e., for loop while loop do-while loop. In this tutorial, you will learn all about for loop in Java. Start now!