7.8 Flow Control in Java: Repeating Loops

Repetition loops, also known as loops, are fundamental structures in programming that allow you to execute a block of code repeatedly as long as a specified condition is true. In Java, there are three main types of repetition loops: for, while and do-while. Let's explore each of them, understand how they work and learn how to apply them efficiently.

Loop for

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

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

Where:

  • Initialization: It is executed once, before the loop starts. Typically, it is used to define the starting point of the loop.
  • Condition: It is evaluated before each iteration. If true, the block of code inside the loop is executed. If false, the loop ends.
  • Increment: It is executed after each iteration of the code block. It is generally used to update the loop counter.

Example of a for loop:

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

In this example, the loop will print the numbers from 0 to 9. The variable i is initialized to 0, and at each iteration, it is incremented by 1 until the condition i < 10 is no longer true.

while

loop

The while loop is used when the number of iterations is not necessarily known in advance. The condition is tested before the code block is executed, and if it is true, the block is executed. The syntax of the while loop is:

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

Example of a while loop:

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

In this case, the loop will work in a similar way to the previous example with for, but the initialization and increment are done outside the loop declaration.

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, as the condition is tested after execution of the code block. The syntax of the do-while loop is:

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

Example of a do-while loop:

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

In this example, the code block inside do will be executed and then the condition i < 10 will be tested. The loop will continue until the condition becomes false.

Loop Control

In addition to the stop condition, Java offers two important keywords to control the execution of loops: break and continue.

  • Break: It is used to exit a loop immediately, regardless of the stopping condition.
  • Continue: It is used to skip the current iteration of the loop and continue with the next iteration.

Example of using break:

for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Exit the loop when i is equal to 5
    }
    System.out.println("Value of i: " + i);
}

Example of using continue:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continues; // Skip to next iteration if i is even
    }
    System.out.println("Value of i: " + i);
}

Conclusion

Repeat loops are powerful tools that allow programmers to execute blocks of code multiple times without having to write the same code repeatedly. Understand and know how to use the for, while and do-while loops, as well as the break keywords and continue, is essential for anyone who wants to learn to program in Java.

Practical writing and reading these loops, in addition to understanding their differences and applicability, will help develop programming logic and write more efficient and concise code. As you advance in your study of Java, you will discover that loops are fundamental in many aspects, from manipulating arrays and collections to processing data and building interactive applications.

Now answer the exercise about the content:

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

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

You missed! Try again.

Article image Flow control (if, else, switch, loops): For loop

Next page of the Free Ebook:

48Flow control (if, else, switch, loops): For loop

4 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