mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-08-31 01:14:08 +08:00
fix(curriculum): add section headers to loops lecture (#69747)
This commit is contained in:
+8
@@ -13,6 +13,8 @@ An example of a loop would be when you are designing a program that needs to pri
|
||||
|
||||
Another example would be when you are designing a game and you want to move a character across the screen. You could use a loop to move the character a certain number of pixels each time the loop runs.
|
||||
|
||||
## The `for` Loop Syntax
|
||||
|
||||
In JavaScript, there are several types of loops that you can use. In this lesson, we will cover the `for` loop. Here is the basic syntax for a `for` loop:
|
||||
|
||||
```js
|
||||
@@ -29,6 +31,8 @@ If the condition is true, the code block inside the loop is executed. If the con
|
||||
|
||||
The last part of the loop is the increment/decrement statement. This statement is executed after each iteration of the loop. It is typically used to increment or decrement the counter variable.
|
||||
|
||||
## A `for` Loop Example
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
@@ -51,8 +55,12 @@ Then we check the condition again which is to check if `i` is less than `5`. Sin
|
||||
|
||||
We keep repeating this process until the condition is false. In this case, when `i` is `5`, the condition is false, and the loop stops.
|
||||
|
||||
## Infinite Loops
|
||||
|
||||
When you're working with loops you should be careful not to create a condition that is always true. If you do, the loop will run forever and your program will crash. This is known as an infinite loop.
|
||||
|
||||
## Nested Loops
|
||||
|
||||
It is possible to create nested `for` loops. A nested loop is when you place one loop inside of another. We will see examples of when you might want to do this later on.
|
||||
|
||||
Loops can be beneficial in programming when you need to repeat a block of code a certain number of times. Even though working with `for` loops can be tricky at first, with practice you will get the hang of it.
|
||||
|
||||
Reference in New Issue
Block a user