codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

16 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java For Loop
☕Java Programming

Java For Loop

Updated 2026-05-12
30 min read

Java For Loop

In the previous tutorial, we explored the while loop, which is used to execute a block of code repeatedly as long as a specified condition is true. In this tutorial, we will dive into another fundamental control structure in Java: the for loop. We'll also cover nested loops and the enhanced for-each loop, providing you with a comprehensive understanding of looping constructs in Java.

Introduction

Loops are essential in programming as they allow us to repeat tasks without writing repetitive code. The for loop is one of the most commonly used loops in Java due to its concise syntax and versatility. Understanding how to use different types of loops will greatly enhance your ability to write efficient and effective Java programs.

Core Content

1. Basic For Loop

The basic for loop in Java consists of three parts: initialization, condition, and update. It is typically used when the number of iterations is known beforehand.

Syntax

Java
1for (initialization; condition; update) {
2 // code block to be executed
3};
  • Initialization: This part is executed only once at the beginning of the loop. It is usually used to initialize a loop counter.
  • Condition: This part is evaluated before each iteration. If it evaluates to true, the loop continues; if false, the loop terminates.
  • Update: This part is executed after each iteration, typically used to update the loop counter.

Example

Let's print numbers from 1 to 5 using a basic for loop.

ForLoopExample.java
1public class ForLoopExample {
2 public static void main(String[] args) {
3 for (int i = 1; i <= 5; i++) {
4 System.out.println(i);
5 }
6 }
7}
Output
1
2
3
4
5

2. Nested For Loops

A nested loop is a loop inside another loop. The inner loop will be executed completely for each iteration of the outer loop.

Example

Let's print a multiplication table using nested for loops.

NestedForLoopExample.java
1public class NestedForLoopExample {
2 public static void main(String[] args) {
3 int rows = 5;
4 for (int i = 1; i <= rows; i++) {
5 for (int j = 1; j <= i; j++) {
6 System.out.print(j + " ");
7 }
8 System.out.println();
9 }
10 }
11}
Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

3. For-Each Loop

The for-each loop, also known as the enhanced for loop, is used to iterate over elements of an array or collection without using a traditional index.

Syntax

Java
1for (element : array) {
2 // code block to be executed
3}
  • Element: This variable holds the value of each element in the array or collection.
  • Array: The array or collection you want to iterate over.

Example

Let's print elements of an array using a for-each loop.

ForEachLoopExample.java
1public class ForEachLoopExample {
2 public static void main(String[] args) {
3 int[] numbers = {1, 2, 3, 4, 5};
4 for (int number : numbers) {
5 System.out.println(number);
6 }
7 }
8}
Output
1
2
3
4
5

Common Mistakes and Pitfalls

  • Initialization: Ensure that the loop counter is initialized correctly. Forgetting to initialize it can lead to infinite loops.
  • Condition: Always make sure the condition will eventually become false to avoid infinite loops.
  • Update: Properly update the loop counter to prevent infinite loops or unintended behavior.
  • For-Each Loop: The for-each loop cannot be used to modify elements of an array directly. If you need to modify elements, use a traditional for loop.

Practical Example

Let's create a program that calculates the factorial of a number using a for loop.

FactorialExample.java
1public class FactorialExample {
2 public static void main(String[] args) {
3 int number = 5;
4 long factorial = 1;
5 for (int i = 1; i <= number; i++) {
6 factorial *= i;
7 }
8 System.out.println("Factorial of " + number + " is: " + factorial);
9 }
10}
Output
Factorial of 5 is: 120

Summary

ConceptDescription
Basic For LoopExecutes a block of code repeatedly based on a condition.
Nested LoopsAllows one loop to be inside another, useful for multi-dimensional structures.
For-Each LoopSimplifies iteration over arrays and collections without using indices.

What's Next?

In the next tutorial, we will explore how to control the flow of loops using break and continue statements. These keywords provide more flexibility in managing loop execution based on specific conditions.

Stay tuned for more Java tutorials!


PreviousJava While LoopNext Java Break/Continue

Recommended Gear

Java While LoopJava Break/Continue