for Loop

The for loop is used to repeat a block of code a specific number of times. It is particularly useful when you know the exact number of iterations beforehand. A for loop includes initialization, a termination condition, and an update statement all in one line, making it a concise choice for counting loops.

Syntax of for Loop

for (initialization; condition; update) {
    // Code to execute
}
  • Initialization: Sets up a loop control variable (usually a counter), executed only once at the beginning of the loop.
  • Condition: A boolean expression evaluated before each iteration. If true, the loop continues; if false, the loop stops.
  • Update: Changes the loop control variable after each iteration, often incrementing or decrementing the counter.

Example of a for Loop

for (int i = 0; i < 5; i++) {
    System.out.println("Count is: " + i);
}

In this example, i starts at 0 and increments by 1 after each iteration. The loop continues while i < 5, so it prints numbers from 0 to 4.

Key Points about for Loop

  • Compact Structure: Initialization, condition, and update are all in one line, making the loop easy to manage for counting.
  • Flexible Usage: Commonly used for loops where you know the number of iterations in advance, like iterating through a range of numbers.

Enhanced for Loop (For-Each Loop) – Iterating Over Arrays and Collections

The Enhanced for loop, or For-Each loop, is designed for iterating through elements in an array or any Collection (like lists, sets, etc.) without using an explicit counter. It provides a clean and readable way to access each element directly, particularly when you don’t need to modify the index or control variable.

Syntax of Enhanced for Loop

for (elementType element : collection) {
    // Code to execute with element
}
  • elementType: The data type of elements in the collection or array.
  • element: A temporary variable representing each element in the array or collection during the iteration.
  • collection: The array or Collection to iterate through.

Example of an Enhanced for Loop with an Array

int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
    System.out.println("Number is: " + num);
}

In this example, each element in the numbers array is stored in num as the loop iterates, printing each number from 1 to 5.

Key Points about Enhanced for Loop

  • Simplicity: It simplifies code by removing the need to manually manage an index variable.
  • Readability: Especially useful when iterating through arrays or collections without modifying them or needing index positions.
  • Limitations: Does not allow for index-based operations, so it isn’t ideal if you need to modify elements or skip specific elements based on conditions.

Comparison of for Loop and Enhanced for Loop

Featurefor LoopEnhanced for Loop
StructureInitialization, condition, update all in one lineUses element : collection syntax for readability
Best forKnown number of iterations, index-based operationsIterating through each element in arrays or collections
ControlFull control over index variableDirect access to elements only, no index
ModificationsCan modify the index or elementsNot suited for modifying or skipping elements directly