mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-12 10:00:39 -04:00
feat(curriculum): Add interactive examples to What Are the Break and Continue Statements Used for in Loops lesson (#63338)
This commit is contained in:
@@ -5,12 +5,14 @@ challengeType: 19
|
||||
dashedName: what-are-the-break-and-continue-statements-used-for-in-loops
|
||||
---
|
||||
|
||||
# --description--
|
||||
# --interactive--
|
||||
|
||||
A `break` statement is used to exit a loop early, while a `continue` statement is used to skip the current iteration of a loop and move to the next one.
|
||||
|
||||
Here is an example of using a `break` statement in a `for` loop:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (i === 5) {
|
||||
@@ -20,6 +22,8 @@ for (let i = 0; i < 10; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
In the example above, the loop starts counting at `0` and while `i` is less then `10`, the loop will continue to run.
|
||||
|
||||
Inside the loop, we check if `i` is equal to `5`. If it is, we use the `break` statement to exit the loop early. If not, we log the value of `i` to the console. So the output of the code will print the numbers `0`, `1`, `2`, `3`, and `4`.
|
||||
@@ -28,6 +32,8 @@ The `break` statement is useful when you want to exit a loop early based on a ce
|
||||
|
||||
Sometimes you may want to skip a particular iteration of a loop without exiting the loop entirely. This is where the `continue` statement comes in. Here is an example of using a `continue` statement in a `for` loop:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (i === 5) {
|
||||
@@ -37,6 +43,8 @@ for (let i = 0; i < 10; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
Just like before, we have initialized `i` to `0` and have a condition that will run the loop as long as `i` is less than `10`.
|
||||
|
||||
Inside the loop, when `i` is equal to `5`, we use the `continue` statement to skip the current iteration and move to the next one.
|
||||
@@ -49,6 +57,8 @@ This is useful when you have nested loops and you want to control the flow of th
|
||||
|
||||
Here is an example of using labels with the `break` statement:
|
||||
|
||||
:::interactive_editor
|
||||
|
||||
```js
|
||||
outerLoop: for (let i = 0; i < 3; i++) {
|
||||
innerLoop: for (let j = 0; j < 3; j++) {
|
||||
@@ -60,6 +70,8 @@ outerLoop: for (let i = 0; i < 3; i++) {
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
In this example, we have an outer `for` loop labeled `outerLoop` and an inner `for` loop labeled `innerLoop`.
|
||||
|
||||
When `i` is equal to `1` and `j` is equal to `1`, we use the `break` statement with the `outerLoop` label to exit the outer loop early. This will exit both the inner and outer loops.
|
||||
@@ -184,3 +196,4 @@ Labels are used to control the flow of execution in nested loops.
|
||||
## --video-solution--
|
||||
|
||||
1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user