DEV Community

Cover image for Java Iteration Tutorial: How to Loop Through an Array in Java
Isah Jacob
Isah Jacob

Posted on

Java Iteration Tutorial: How to Loop Through an Array in Java

An Arrayis a collection of variables having values of the same type. Because Arraysare objects, they are reference types. An Arrayis essentially a memory reference to an Arrayobject. To refer to a specific element in an Array, we supply the name of the Arrayreference and the element's position number in the array. The element's index is the element's position number.

What Are Loops In Java

Java has looping statements, which allow programs to repeat statements as long as a condition is true. The first step in creating a Java loop is defining the statement that will be looped. In the second phase, the statement is wrapped in a loop. We'll go through the most frequent ones in this article so you can learn and understand how they function.

double[] grades = {96.5, 98.5, 99.3, 67, 90.1, 4, 67 };
Enter fullscreen mode Exit fullscreen mode

The While Loop

A while loop operates in such a way that it repeats statements repeatedly while the condition is true. The loop and program are ended if the condition is false.
Let's build a loop and provide the statement that will be looped.

int i = 0;
while (i <= 10){
    System.out.println(grades[i]);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

This will print out the element in each index

96.5
98.5
99.3
67.0
90.1
4.0
67.0
34.0
54.0
12.9
Enter fullscreen mode Exit fullscreen mode

We output the students' grade values from 1 to 10 in the loop above. We start with i=0 and increment it with i++. Because the last index is 10, the loop condition will terminate execution when i = 10. If we set i=10 to true, the loop will loop indefinitely.

The Do-While-Loop

The major difference between a whileloop and a do-while loop is that the latter executes the loop body to determine whether the loop should be stopped or maintained.
Let's take a closer look at how do-while loops work.

do {
    System.out.println(grades[i]);
    i++;
} while (i <= 10);
Enter fullscreen mode Exit fullscreen mode

this will print

96.5
98.5
99.3
67.0
90.1
4.0
67.0
34.0
54.0
12.9
Enter fullscreen mode Exit fullscreen mode

In a do-while loop, if the condition is true, the loop body is executed i = 10 and control updates the expression by either incrementing or decrementing i. If the condition is false, the loop breaks automatically.

The For Loop

A for-loop provides a simplified syntax for creating loops. A loop is frequently written in the following format:

for (int j = 0; j <= 10; j++) {
    System.out.println(grades[j]);
}
Enter fullscreen mode Exit fullscreen mode

This will print out the following

96.5
98.5
99.3
67.0
90.1
4.0
67.0
34.0
54.0
12.9
Enter fullscreen mode Exit fullscreen mode

The for loop sets condition to 0, then repeats the println command and checks condition++ if condition is less than 10. The loop continuation condition is represented by Condition 10. The condition is verified both at startup and at the beginning of each iteration. If this condition is met, the loop body is performed. If it is false, the loop is terminated and the program control is transferred to the line after the loop.

For Each Loop in Java.

To loop through arrayelements, the for-each loop is utilized. It is sometimes referred to as the enhanced-loop.
Let's have a look at how it works.

for (double grade: grades) {
    System.out.println(grade);
}
Enter fullscreen mode Exit fullscreen mode

the out put of this would be the following

96.5
98.5
99.3
67.0
90.1
4.0
67.0
34.0
54.0
12.9
Enter fullscreen mode Exit fullscreen mode

What differentiates for-each loop from every other is, here, we print each elements of grades one by one. It is not quite different from other loops we have discussed.

Finishing up

Finally, because of the continuation condition, while and for-loopsare referred to as "pretest loops," which means they are checked before the loop body is executed. The do-while loop is known as a "posttest loop." This also indicates that the condition is checked once the loop body is finished. The three loop various types are interchangeable.

A loop of any of these loop types, such as a while-loop, may always be modified into a for-loop or enhance-loop. While, do-while, and for-loop all express the same thing and can be used interchangeably.

Top comments (0)