Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Avoiding Infinite Loops | Applying Logic and Writing Clean Code
Conditional Statements and Loops in JavaScript

bookAvoiding Infinite Loops

When you write code that repeats actions, you want it to eventually stop. An infinite loop is like getting stuck in a revolving door: you keep going around and around, but never find the exit. In programming, this happens when a loop never meets its stopping condition, causing your program to run forever or until it crashes. Understanding why infinite loops happen is important for writing safe and reliable code.

// Example of an infinite loop:
let counter = 1;
while (counter < 5) {
  // This line is missing: counter++;
  console.log("Counter is: " + counter);
}
// This loop will never end because 'counter' is never updated.
123456
// Fixed version: let safeCounter = 1; while (safeCounter < 5) { console.log("Counter is: " + safeCounter); safeCounter++; // Now the counter changes, and the loop will stop at 5 }
copy

1. Which change will prevent the following loop from running forever?

2. Fill in the blank to ensure this loop stops after printing numbers 1 to 4.

question mark

Which change will prevent the following loop from running forever?

Select the correct answer

question-icon

Fill in the blank to ensure this loop stops after printing numbers 1 to 4.

let num = 1; while (num < 5) { console.log(num); }
1
2
3
4

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 7.69

bookAvoiding Infinite Loops

Desliza para mostrar el menú

When you write code that repeats actions, you want it to eventually stop. An infinite loop is like getting stuck in a revolving door: you keep going around and around, but never find the exit. In programming, this happens when a loop never meets its stopping condition, causing your program to run forever or until it crashes. Understanding why infinite loops happen is important for writing safe and reliable code.

// Example of an infinite loop:
let counter = 1;
while (counter < 5) {
  // This line is missing: counter++;
  console.log("Counter is: " + counter);
}
// This loop will never end because 'counter' is never updated.
123456
// Fixed version: let safeCounter = 1; while (safeCounter < 5) { console.log("Counter is: " + safeCounter); safeCounter++; // Now the counter changes, and the loop will stop at 5 }
copy

1. Which change will prevent the following loop from running forever?

2. Fill in the blank to ensure this loop stops after printing numbers 1 to 4.

question mark

Which change will prevent the following loop from running forever?

Select the correct answer

question-icon

Fill in the blank to ensure this loop stops after printing numbers 1 to 4.

let num = 1; while (num < 5) { console.log(num); }
1
2
3
4

Click or drag`n`drop items and fill in the blanks

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
some-alt