Iterate using for Loop

The for statement provides a way to iterate over a range of values.

Setup

double[] numbers = {55, 29, 68, 21, 48, 36,   92,  56,  88,  96};

Infinite for loop

@Ignore
@Test
public void loop_for_infinite () {
    
    for ( ; ; ) {
        System.out.println("don't run this code");
    }
}

Java 1.4 for

@Test
public void loop_for_java_1_4 () {
    
    for (int x = 0; x < numbers.length; x ++) {
        System.out.println(numbers[x]);
    }
}

Java 1.5 for each

@Test
public void loop_for_java_1_5 () {
    
    for (double number : numbers) {
        System.out.println(number);
    }
}

Java 8

@Test
public void loop_for_java_1_8 () {
    
    Arrays.stream(numbers).forEach(x -> System.out.println(x));
    
}