For Loop Vs For each Loop in Java

[su_table]

For LoopFor each Loop
Both increment and decrement can be doneOnly Increment iteration can be done
Need variable declarationOnly temp variable is needed

[/su_table]

Here the sample program [su_highlight]JavaLoops.java[/su_highlight]

package com.javatraineronline;

public class JavaLoops {

	public static void main(String[] args) {
		
		// Declaring the String Array.
		String[] strArray = {"Aa", "Bb", "Cc", "Dd", "Ee"};
		
		System.out.println("Traditional For Loop");
		System.out.println("--------------------");
		
		// Iterating the array using Traditional for loop
		for (int i = 0; i < strArray.length; i++) {
			
			System.out.print(strArray[i]+" ");
		}
		
		System.out.println("\n");
		System.out.println("For Each Loop");
		System.out.println("-------------------");
		
		// Iterating the array using for each loop.
		for (String temp : strArray) {
		
			System.out.print(temp+" ");
		}
	}
}

[su_box title=”Output for JavaLoops.java”]Traditional For Loop
——————–
Aa Bb Cc Dd Ee

For Each Loop
——————-
Aa Bb Cc Dd Ee [/su_box]