Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. The for-each loop (also called the enhanced for loop) in Java provides a concise way to iterate over arrays or collections without using an index variable. It was introduced in Java 5 and is ideal for read-only traversal.

    Example – Iterating Over an Array

    public class Example {
    public static void main(String[] args) {
    int[] numbers = {1, 2, 3, 4, 5};

    // For-each loop
    for (int num : numbers) {
    System.out.println(num);
    }
    }
    }
    Copied!

    Output:

    1
    2
    3
    4
    5
    Copied!

    Here, num takes the value of each element in numbers sequentially, eliminating the need for manual indexing.

    Syntax

    for (datatype variable : arrayOrCollection) {
    // use variable
    }
    Copied!
    • Works with arrays and any object implementing Iterable (e.g., List, Set).

    • Internally uses an iterator for collections and an index counter for arrays.

    Example – Iterating Over a List

    import java.util.*;

    public class Example {
    public static void main(String[] args) {
    List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");

    for (String fruit : fruits) {
    System.out.println(fruit);
    }
    }
    }
    Copied!
    Feedback
  2. Java For Loop - GeeksforGeeks

    Mar 11, 2026 · The for loop in Java is a control flow statement used to execute a block of code repeatedly based on a condition. It is especially useful when the …

  3. The for Statement (The Java™ Tutorials > Learning the Java ... - Oracle

    The for Statement The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a …

  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. Check if a variable is between two numbers with Java

    And, like others have said, Java doesn't have an elegant syntax for checking if a …

    • Reviews: 6
    • Printing Even Numbers with Java Using Loops - Medium

      Jul 14, 2025 · Looping over a range of numbers and filtering for even values is one of the simplest but most effective tasks for learning control flow in Java. It lets …

    • People also ask
      Loading
      Unable to load answer
    • Java For Loop - Baeldung

      Feb 25, 2026 · A for loop is a control structure that allows us to repeat certain operations by incrementing and evaluating a loop counter. Before the first …

      Missing:
      • Numbers
      Must include:
    • Mastering the `for` Loop in Java - javaspring.net

      Jan 16, 2026 · The for loop is a powerful and versatile construct in Java that allows you to iterate over a sequence of values. By understanding its fundamental concepts, usage methods, common practices, …

    • How to Write a Java Statement to Display Integers Between Two Given ...

      Learn how to create a Java statement that prints all integers between two specified numbers with a step-by-step guide and code example.

    • Loops in java - For, While, Do-While Loop in Java

      This Java program generates a triangle pattern with numbers increasing sequentially from 1 to the row number. The outer loop is a for loop, while the …