Basic Java Syntax: Repeating Loops

Repeat loops are fundamental structures in any programming language, including Java. They allow a block of code to be executed repeatedly, which is essential for tasks such as processing collections of data, executing operations until a condition is met, among others. In Java, the main repetition loops are for, while and do-while.

The Lasso for

The for loop is often used when the number of iterations is known. The basic syntax of the for loop in Java is as follows:

for (initialization; condition; increment) {
    // Block of code to be repeated
}

The initialization is executed only once, before the loop begins. The condition is evaluated before each iteration and, if true, the code block is executed. After each iteration, the increment is performed, and the condition is evaluated again. The loop ends when the condition becomes false.

Example:

for (int i = 0; i < 10; i++) {
    System.out.println("The value of i is: " + i);
}

In this example, the for loop prints the numbers from 0 to 9. The variable i is initialized to 0, the condition checks if i is less than 10, and i is incremented by 1 at each iteration.

The while

Loop

The while loop is used when the number of iterations is not known in advance, and the loop must continue as long as a condition is true. The syntax of the while loop is:

while (condition) {
    // Block of code to be repeated
}

The condition is evaluated before each iteration. If true, the code block is executed. Otherwise, the loop is terminated.

Example:

int i = 0;
while (i < 10) {
    System.out.println("The value of i is: " + i);
    i++;
}

In this example, the while loop also prints the numbers 0 to 9. The variable i is initialized outside the loop, the condition checks whether i< /code> is less than 10, and i is incremented inside the loop.

The do-while Loop

The do-while loop is a variation of the while loop that ensures that the code block is executed at least once, regardless of the condition. The syntax of the do-while loop is:

do {
    // Block of code to be repeated
} while (condition);

The code block is executed first, and the condition is evaluated after execution. If the condition is true, the loop continues; if false, the loop ends.

Example:

int i = 0;
of {
    System.out.println("The value of i is: " + i);
    i++;
} while (i < 10);

In this example, the do-while loop also prints the numbers 0 to 9. The i variable is initialized outside the loop, the code block is executed, i is incremented, and then the condition is checked.

Important Considerations

It is crucial to be careful with the stopping condition of the loops to avoid creating infinite loops, which can crash the program. Additionally, it is possible to use control statements within loops, such as break, to exit the loop immediately, and continue, to skip to the next iteration.

Loops can also be nested, that is, one loop can be placed inside another. This is useful for working with multidimensional data structures, such as two-dimensional arrays (arrays).

Example of nested loops:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.println("Coordinates: [" + i + "][" + j + "]");
    }
}

In this example, the outer loop controls the variable i, while the inner loop controls the variable j. The result is the printout of the coordinates of a 3x3 matrix.

In conclusion, the for, while and do-while loops are powerful tools in the Java language that allow repeated execution of blocks of code. Understanding its syntax and operation is essential for any programmer who wants to create efficient and sophisticated programs in Java.

Now answer the exercise about the content:

_Which of the following statements is true about repetition loops in Java?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Java Basic Syntax: Arrays

Next page of the Free Ebook:

10Java Basic Syntax: Arrays

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text