Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
For Loops | Loops
Ninja Game
course content

Conteúdo do Curso

Ninja Game

Ninja Game

1. Basic Controls
2. Functions
3. Loops
4. If-Else Statements
5. Challenges

book
For Loops

In this chapter, we will explore the concept of loops, specifically focusing on "for loops." Loops are a fundamental concept in programming that allow us to repeat a block of code multiple times without having to write it out repeatedly. This can be incredibly useful when you want to perform the same action multiple times, such as moving our ninja across the grid to collect sushi.

What is a For Loop?

A "for loop" is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop will continue to execute as long as the condition is true. Once the condition is false, the loop will stop.

The basic syntax of a for loop in JavaScript is as follows:

123
for (let i = 0; i < 5; i++) { console.log("Iteration:", i) }
copy
  • Initialization(let i = 0): This is where you initialize your loop variable. It is executed once at the beginning of the loop.
  • Condition(i < 5): Before each iteration, the condition is evaluated. If it is true, the loop continues. If it is false, the loop stops.
  • Increment(i++): This is executed after each iteration of the loop. It is typically used to update the loop variable.

Let's see an example of a for loop in action with our ninja:

js

ninja.js

html

index.html

js

preset.js

copy

In this example, the ninja will move up and pick sushi six times. The loop variable i starts at 0 and increments by 1 each time the loop runs, stopping when i reaches 6.

Tarefa
test

Swipe to show code editor

Goal: Collect all sushi.

To complete this task, you will need to use a for loop to move the ninja and pick up all the sushi on the grid. Consider how many times you need to repeat the actions and how you can use the loop to achieve this efficiently.

Solução

Starter Map

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
js

ninja.js

html

index.html

js

preset.js

toggle bottom row

book
For Loops

In this chapter, we will explore the concept of loops, specifically focusing on "for loops." Loops are a fundamental concept in programming that allow us to repeat a block of code multiple times without having to write it out repeatedly. This can be incredibly useful when you want to perform the same action multiple times, such as moving our ninja across the grid to collect sushi.

What is a For Loop?

A "for loop" is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop will continue to execute as long as the condition is true. Once the condition is false, the loop will stop.

The basic syntax of a for loop in JavaScript is as follows:

123
for (let i = 0; i < 5; i++) { console.log("Iteration:", i) }
copy
  • Initialization(let i = 0): This is where you initialize your loop variable. It is executed once at the beginning of the loop.
  • Condition(i < 5): Before each iteration, the condition is evaluated. If it is true, the loop continues. If it is false, the loop stops.
  • Increment(i++): This is executed after each iteration of the loop. It is typically used to update the loop variable.

Let's see an example of a for loop in action with our ninja:

js

ninja.js

html

index.html

js

preset.js

copy

In this example, the ninja will move up and pick sushi six times. The loop variable i starts at 0 and increments by 1 each time the loop runs, stopping when i reaches 6.

Tarefa
test

Swipe to show code editor

Goal: Collect all sushi.

To complete this task, you will need to use a for loop to move the ninja and pick up all the sushi on the grid. Consider how many times you need to repeat the actions and how you can use the loop to achieve this efficiently.

Solução

Starter Map

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt