Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java For Loop
Next article icon

Java for loop vs Enhanced for loop

Last Updated : 03 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, loops are fundamental constructs for iterating over data structures or repeating blocks of code. Two commonly used loops are the for loop and the enhanced for loop. While they serve similar purposes, their applications and usage vary based on the scenario.

for loop vs Enhanced for loop

Below comparison highlights the major differences between a traditional for loop and an enhanced for loop in Java.

Parametersfor loopEnhanced for loop
IntroducedIntroduced in early Java versions as a basic loop structure.Introduced in Java 5 as a simplified version for iterating over collections/arrays.
UsageGeneral-purpose loop suitable for arrays, collections, and numerical ranges.Specifically designed for iterating over arrays and collections.
Index-Based AccessAllows access to elements by their index using i.No index-based access; iterates directly over elements.
Read-Only IterationSupports both read and modify operations on elements.Primarily for read-only operations. Modification may lead to errors.
Control Over IterationOffers more control over iteration (start, end, and step).Limited control as it automatically iterates over all elements.
Syntax ComplexitySlightly verbose; requires initialization, condition, and increment/decrement logic.Concise and simpler to write.
Best Use CaseUseful when indices or custom iteration logic is required.Ideal for directly processing all elements of an array or collection.
PerformanceMay perform slightly better when accessing array elements directly by index.Performance depends on the internal iteration mechanism of the collection.

for loop in Java

The traditional for loop is a versatile structure used to iterate over a range of values, arrays, or other data structures. It provides complete control over the iteration process for any tasks which need to do multiple times.

Syntax:

for( initialization; conditional_check ; increment_or_decrement_section)
{
// Code to be executed which defined in the conditions
}

Example:

Java
public class Geeks {        public static void main(String[] args) {                int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};                // Iterate through the array using a for loop         for (int i = 0; i < n.length; i++) {             System.out.println("Index " + i + ": " + n[i]);         }     } } 

Output
Index 0: 1 Index 1: 2 Index 2: 3 Index 3: 4 Index 4: 5 Index 5: 6 Index 6: 7 Index 7: 8 Index 8: 9 Index 9: 10 

Enhanced for loop (for-each loop) 

The enhanced for loop, also known as the for-each loop, is a streamlined way to iterate over collections and arrays. It eliminates the need for managing the loop's counter or bounds manually.

Syntax:

for (data_Type_element : array_Or_Collection) {
// Code to be executed which defined in the conditions
}

Example:

Java
import java.io.*; import java.util.*;  class Geeks {     public static void main(String[] args) {         int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };                // Iterate through the array          // using an enhanced for loop         for (int a : arr) {             System.out.println(a);         }     } } 

Output
1 2 3 4 5 6 7 8 9 10 

Next Article
Java For Loop

M

mroshanmishra0072
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Difference Between
  • Technical Scripter 2020
  • Java-Loops
Practice Tags :
  • Java

Similar Reads

  • For-Each Loop in Java
    The for-each loop in Java (also called the enhanced for loop) was introduced in Java 5 to simplify iteration over arrays and collections. It is cleaner and more readable than the traditional for loop and is commonly used when the exact index of an element is not required. Example: Using a for-each l
    8 min read
  • String Array with Enhanced For Loop in Java
    Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence it is mainly used for traversing arrays or collections.
    1 min read
  • Java - Loop Through an Array
    In Java, looping through an array or Iterating over arrays means accessing the elements of the array one by one. We have multiple ways to loop through an array in Java. Example 1: Here, we are using the most simple method i.e. using for loop to loop through an array. [GFGTABS] Java // Java program t
    3 min read
  • Java For Loop
    Java for loop is a control flow statement that allows code to be executed repeatedly based on a given condition. The for loop in Java provides an efficient way to iterate over a range of values, execute code multiple times, or traverse arrays and collections. Now let's go through a simple Java for l
    5 min read
  • Difference between forEach and for loop in Javascript
    In JavaScript, both forEach and for loops are used to iterate over arrays or collections, but they differ in usage and behavior. forEach is a higher-order function that simplifies iteration with built-in array methods, while for loops offer more control, flexibility, and broader application. For Loo
    4 min read
  • How to Loop Over TreeSet in Java?
    TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co
    4 min read
  • Java Do While Loop
    Java do-while loop is an Exit control loop. Unlike for or while loop, a do-while check for the condition after executing the statements of the loop body. Example: [GFGTABS] Java // Java program to show the use of do while loop public class GFG { public static void main(String[] args) { int c = 1; //
    4 min read
  • JavaScript for...of Loop
    The JavaScript for...of loop is a modern, iteration statement introduced in ECMAScript 2015 (ES6). Works for iterable objects such as arrays, strings, maps, sets, and more. It is better choice for traversing items of iterables compared to traditional for and for in loops, especially when we have bre
    3 min read
  • Difference between for and while loop in C, C++, Java
    In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let's understand the basic differences between a for loop and a while loop. for Loop A for loop prov
    5 min read
  • Difference between for and do-while loop in C, C++, Java
    for loop: for loop provides a concise way of writing the loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping. Syntax: for (initialization condition; testing con
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences