Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Foreach Loop In JAVA
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. Java Tutorials
  3. Foreach Loop In JAVA

Java Tutorial

Basic JAVA Fundamentals

  • JAVA Tutorial Content
  • What Is Java
  • History Of Java
  • Java Environment
  • Features Of JAVA
  • C++ Vs Java
  • Java Basic Syntax
  • First Program Of Java
  • Java Identifiers
  • Java Modifiers
  • Java Variable Types
  • Java Basic Datatypes
  • Java Keywords

Control Statements

  • Control Statements
  • Java If Else Statement
  • Nested If Statement In Java
  • Java Switch Statement

JAVA Loops

  • Loop In Java
  • For Loop In JAVA
  • Foreach Loop In JAVA

Foreach Loop In JAVA

Java For-each or Enhanced For Loop

The for-each loop is used to traverse array or collection in java. It is easier to use than simple for a loop because we don't need to increment value and use subscript notation.

It works on elements basis not index. It returns elements one by one in the defined variable.

Syntax : 

for(Type var:array){  
//code to be executed 
//syntax for foreach loop 
}  

Example Foreach Loop 1:

public class ForEachDemo   
{  
    public static void main(String[] args)   
    {  
        int[] arr = { 1, 2, 3, 4 };  
        for (int i : arr)   
        {  
            System.out.println(i);  
        }  
    }  
}  

OutPut

1
2
3
4

Example Foreach : (PROGRAM TO PRINT ARRAY VALUES WITH USER INPUT)

//PROGRAM TO PRINT ARRAY VALUES WITH SCANNER
import java.util.Scanner;  
public class ForEachDemo   
{  
    public static void main(String arng[]) {  
        int value[] = new int[5];  
        Scanner data = new Scanner(System.in);  
        float sum = 0;  
        System.out.println("Enter 5 element of array");  
        // Enhanced for loop  
        for (int i : value) {  
            i = data.nextInt();  
            sum = sum + i;  
        }  
        System.out.println("Sum is " +sum);  
    }  
}  

Output :

Enter 5 element of array
5
5
5
5
5

Sum is 25.0

 

 

Series: Java Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
java for loop array java while loop example for loop programs in java with output java for loop list java for loop iterator enhanced for loop in java example java for each loop

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.