Conteúdo do curso
JavaScript Course for Beginners: Fundamentals and Practice
Sobre a Aula

Repetition structures: while, do-while, for

Welcome to the second lesson of Module 1! In this journey, you’ll explore the power of loop structures in JavaScript.

Get ready to learn how to execute code blocks repeatedly, automating tasks and creating dynamic functionalities in your scripts.

while Structure

The while structure executes a code block repeatedly while a specified condition is true. It’s like running continuously until you have completed all laps.

Syntax:

while (condition) {
  // code block to be repeated while the condition is true
}

Example:

let i = 1;
let sum = 0;

while (i <= 10) {
  sum += i;
  i++;
}

console.log("The sum of numbers from 1 to 10 is: " + sum);

do-while Structure

The do-while structure is similar to while, but with an important difference.

The code block is executed at least once before checking the condition.

It’s like running at least one lap before checking if you still need to continue running.

Syntax:

do {
  // code block to be repeated at least once
} while (condition);

Example:

let i = 1;
let sum = 0;

do {
  sum += i;
  i++;
} while (i <= 10);

console.log("The sum of numbers from 1 to 10 is: " + sum);

for Structure

The for structure is used when you know exactly how many times you want to repeat a code block.

It’s like deciding to run a specific number of laps on the track.

Syntax:

for (initialization; condition; increment) {
  // code block to be repeated while the condition is true
}

Example:

for (let i = 1; i <= 10; i++) {
  sum += i;
}

console.log("The sum of numbers from 1 to 10 is: " + sum);

Finally, one more example to reinforce understanding of loop structures:

// Example using while
let counter = 0;
while (counter < 5) {
  console.log("Washing dish " + (counter + 1));
  counter++;
}

// Example using do-while
let counter2 = 0;
do {
  console.log("Washing dish " + (counter2 + 1));
  counter2++;
} while (counter2 < 5);

// Example using for
for (let counter3 = 0; counter3 < 5; counter3++) {
  console.log("Washing dish " + (counter3 + 1));
}

Challenge:

  1. Create a JavaScript script that prompts the user for a number.
  2. Use a loop structure to display on the screen the numbers from 1 to the number entered by the user.
  3. Use a loop to calculate the sum of even numbers between 1 and the entered number.
  4. Display the results on the console.

Remember:

  • Loop structures are essential tools to automate tasks and create dynamic functionalities in your scripts.
  • Practice creating different loops with while, do-while, and for to master the use of these structures.
  • Use control variables to manage the loop flow and ensure it terminates at some point.

Now that you understand how to use loop structures, you can create efficient loops in your code to perform repetitive tasks.

In the next lesson, we’ll explore the use of logical operators to perform more complex checks.

Entrar na conversa
Rolar para cima