For Loop Vs For each Loop in Java


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


Here the sample program [su_highlight][/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");
		// Iterating the array using Traditional for loop
		for (int i = 0; i < strArray.length; i++) {
			System.out.print(strArray[i]+" ");
		System.out.println("For Each Loop");
		// Iterating the array using for each loop.
		for (String temp : strArray) {
			System.out.print(temp+" ");

[su_box title=”Output for”]Traditional For Loop
Aa Bb Cc Dd Ee

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