Java For Loop And ForEach Loop Example

Chapter: Control Statements Last Updated: 03-09-2016 12:20:44 UTC

Program:

            /* ............... START ............... */
                
public class JavaForAndForEachLoops {

	public static void main(String[] args) {
		int[] intary = { 1, 2, 3, 4 };
		forDisplay(intary);
		foreachDisplay(intary);
	}

	public static void forDisplay(int[] a) {
		System.out.println("Display an array using for loop");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
	}

	public static void foreachDisplay(int[] data) {
		System.out.println("Display an array using for each loop");
		for (int a : data) {
			System.out.print(a + " ");
		}
	}
}
                /* ............... END ............... */
        

Output

Display an array using for loop
1 2 3 4 
Display an array using for each loop
1 2 3 4 

Tags

For Loop And ForEach Loop Example, Control Statements, Java

Similar Programs Chapter Last Updated
Java Enum And Switch Statement Control Statements 03-09-2016
Control Statements In Java Example Control Statements 19-06-2016
If Statement In Java Example Control Statements 17-06-2016
Java Switch Example Control Statements 14-06-2016
While Loop In Java Example Control Statements 08-06-2017
Return Statement In Java example Control Statements 15-07-2016
Print Stars In Java Example Control Statements 15-07-2016
Nested Loop In Java Example Control Statements 15-07-2016
Infinite While Loop In Java Control Statements 10-06-2016
Infinite Loop In Java Example Control Statements 15-07-2016
If Else Statement In Java Example Control Statements 10-06-2016
For Loop In Java Example Control Statements 07-06-2017
For Each In Java Example Control Statements 07-06-2017
Do While Loop In Java Example Control Statements 10-06-2016
Continue Statement In Java Example Control Statements 30-03-2017
Java Break Statement Example Control Statements 06-07-2016

1