JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java for-each Loop

This is similar to a for loop except that it has some enhanced features. It can be used to iterate over the elements of a collection without knowing the index of each element.

It dosn't need to know the size of the collection.

In a simple word java for loop has an alternative syntax that makes it easy to iterate through arrays and collections.

Syntax:

for(type variableName: arrayName){
// statement or code to be executed
}

Example:

// Java program to print the element of the Array

class Simple{
    public static void main(String args[]){
    //Declaring an array
    int array[] = {11,22,33,44,55,66,77,88,99};

    //Printing array elements using for-each loop
    for(int i:array){
        System.out.println(i);
    }
    }
}

Output:

11
22
33
44
55
66
77
88
99

Example 2: Program to print all the elements in the name array

// Java program to print the element of the Array

class Simple{
    public static void main(String args[]){
    //Declaring an array
    String name[] = {"Raj" , "Raja" , "Ankita" , "Komal" , "Jackson"};
    
    //Printing array elements using for-each loop
    for(String i:array){
        System.out.println(i);
    }
    }
}

Output:

Raj
Raja
Ankita
Komal
Jackson