Comparison of Loop Types in Java: for, for-each, while, and do-while

In Java, loops allow a block of code to run repeatedly based on a condition. Each type of loop is suited for different situations, such as when we know how many times to repeat, want to check a condition before or after running, or simply iterate through a collection.

Java mainly supports four types of loops:

  1. for loop – Used when the number of iterations is known.
  2. for-each loop – Used for iterating over arrays or collections.
  3. while loop – Used when the number of iterations is not known in advance.
  4. do-while loop – Similar to while, but guarantees one execution.

Comparison Table:

Feature/Aspect for Loop for-each Loop (Enhanced for) while Loop do-while Loop
Use case When index/control is needed When reading elements from array/list When loop needs to run on a condition When loop must run at least once
Syntax for (init; cond; update) for (type var : array) while (condition) do { } while (condition);
Condition check Before loop body Internally handled Before loop body After loop body
Minimum execution 0 times (if condition is false) 0 times (if array is empty) 0 times (if condition is false) At least 1 time
Accessing index Yes No Yes (if index manually used) Yes (if index manually used)
Best for Indexed iterations, range-based logic Reading elements from arrays/collections Condition-controlled loops Input/menu that must run once at least
Can modify elements? Yes (using index) Not directly Yes Yes
Readable/Simple? Medium (more code) Very simple Medium Medium
Example
for (int i = 0; i < 5; i++) {
    System.out.println(i);
}
int[] nums = {1, 2, 3};
for (int n : nums) {
    System.out.println(n);
}
int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
int j = 0;
do {
    System.out.println(j);
    j++;
} while (j < 5);

Loop Selection Guide

Situation Best Loop
You need counter/index for loop
You just want to read all values for-each
You want to run while condition is true while loop
You want the loop to run at least once do-while loop

Summary :

  1. for loop – Use when the number of iterations is known and index/control is needed.
  2. for-each loop – Best for reading elements from arrays/collections; no index access.
  3. while loop – Use when condition needs to be checked before running; may not run at all.
  4. do-while loop – Executes loop body at least once; condition is checked after execution.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar