Switch to Bing in English
About 165,000 results
Open links in new tab
  1. 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!

    In 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!

    In 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.

    Feedback
  2. 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.

  3. People also ask
  4. 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.

  5. 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, …

  6. 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:

  7. 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.

  8. 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 …

  9. 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.

  10. 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.

  11. 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!

By using this site you agree to the use of cookies for analytics, personalized content, and ads.Learn more about third party cookies|Microsoft Privacy Policy