9. Repetition Structures (FOR, WHILE)

Página 36

Chapter 9: Repetition Structures (FOR, WHILE)

Iteration structures, also known as loops, are fundamental in programming logic. They allow a certain part of the code to be repeated several times, which is extremely useful in many situations. In this chapter, we'll explore two of the most common looping structures: FOR and WHILE.

FOR

The FOR loop structure is used when we know exactly how many times we want a block of code to be executed. The basic structure of a FOR loop is as follows:

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

The 'initialization' is where we define a control variable for the loop. The 'condition' is the criterion that determines when the loop should stop. And the 'increment' is what happens to the control variable with each repetition of the loop. Let's see an example:

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

In this example, the variable 'i' is initialized with the value 0. As long as 'i' is less than 10, the code inside the loop will be executed and 'i' will be incremented by 1 with each repetition. So this loop will print the numbers from 0 to 9.

WHILE

The WHILE loop structure is used when we don't know how many times the code should be repeated, but we have a condition that determines when the loop should stop. The basic structure of a WHILE loop is as follows:

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

The 'condition' is the criterion that determines when the loop should stop. The code inside the loop will be repeated as long as the condition is true. Let's see an example:

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

In this example, the variable 'i' is initialized to the value 0 outside the loop. As long as 'i' is less than 10, the code inside the loop will be executed and 'i' will be incremented by 1 with each repetition. So this loop will also print the numbers 0 through 9.

It is important to note that we must be careful not to create infinite loops. An infinite loop is a loop that never stops, because the condition always remains true. This can cause many problems, including causing the program to crash. Therefore, we must always ensure that the loop condition eventually becomes false.

In short, the FOR and WHILE loops are powerful tools in logic programming. They allow us to repeat a certain piece of code as many times as we want, based on a condition. This gives us great flexibility and efficiency in solving programming problems.

In the next chapter, we'll explore another important repetition structure: DO-WHILE. Stay tuned!

Now answer the exercise about the content:

What is the main difference between FOR and WHILE looping structures in programming logic?

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

You missed! Try again.

Next page of the Free Ebook:

3710. Vectors and Matrices

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or 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