Free Ebook cover Learn to program in complete Java, from programming logic to advanced

Learn to program in complete Java, from programming logic to advanced

3.5

(4)

238 pages

Basic Java syntax: Loops (for, while, do-while)

Capítulo 9

Estimated reading time: 5 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The do-while loop always executes the code block once before evaluating the condition. This ensures the code is executed at least once, even if the condition is false.

Next chapter

Java Basic Syntax: Arrays

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.
  • Read this course in the app to earn your Digital Certificate!
  • Listen to this course in the app without having to turn on your cell phone screen;
  • Get 100% free access to more than 4000 online courses, ebooks and audiobooks;
  • + Hundreds of exercises + Educational Stories.