[su_table]

For Loop For each Loop
Both increment and decrement can be done Only Increment iteration can be done
Need variable declaration Only 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]

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *