Free Ebook cover Complete Logic Programming Course for Beginners

Complete Logic Programming Course for Beginners

4

(3)

83 pages

Repetition Structures (FOR, WHILE)

Capítulo 36

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

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

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.

The FOR loop is designed for when the number of iterations is known, allowing initialization, condition check, and increment in one line. In contrast, the WHILE loop is preferred when the number of iterations is not predetermined, and it continues until a specified condition is no longer true.

Next chapter

Vectors and Matrices

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.