An Array
is a collection of variables having values of the same type. Because Arrays
are objects, they are reference types. An Array
is essentially a memory reference to an Array
object. To refer to a specific element in an Array
, we supply the name of the Array
reference 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 };
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++;
}
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
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 while
loop 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);
this will print
96.5
98.5
99.3
67.0
90.1
4.0
67.0
34.0
54.0
12.9
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]);
}
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
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 array
elements, 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);
}
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
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-loops
are 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)