Lessons
JavaScript Basics
Operators in JavaScript
Conditional Statements in JavaScript
JavaScript Strings
JavaScript Arrays
JavaScript Loop
JavaScript Functions
Conclusion
JavaScript break and continue Statements
JavaScript break
and continue
statements are used to control the behavior of loops. They allow you to exit a loop or skip specific iterations based on a condition, giving you more control over how the loop works.
The break Statement
The break
statement is used to stop a loop entirely before it would naturally end. When a break
statement is encountered, the program immediately exits the loop and continues executing the code after it.
Syntax
javascript
1
break;
Example: Exiting a Loop
In this example, the loop stops when the value of i
equals 5:
javascript
1 2 3 4 5 6 7 8 9 10
let result = ""; for (let i = 0; i < 10; i++) { if (i === 5) { break; // Exit the loop when i equals 5 } result += "Counting: " + i + "\n"; } console.log(result);
Output:
javascript
1 2 3 4 5
Counting: 0 Counting: 1 Counting: 2 Counting: 3 Counting: 4
Why Use break?
- To stop the loop early when a specific condition is met.
- To save time and resources by avoiding unnecessary iterations.
The continue Statement
The continue
statement skips the rest of the code in the current iteration and moves directly to the next iteration. Unlike break
, it does not stop the loop entirely.
Syntax
javascript
1
continue;
Example: Skipping an Iteration
This example skips the iteration when i
is an even number:
javascript
1 2 3 4 5 6 7 8 9 10
let output = ""; for (let i = 0; i < 10; i++) { if (i % 2 === 0) { continue; // Skip even numbers } output += "Odd number: " + i + "\n"; } console.log(output);
Output:
javascript
1 2 3 4 5
Odd number: 1 Odd number: 3 Odd number: 5 Odd number: 7 Odd number: 9
Why Use continue?
- To skip certain values or conditions without stopping the entire loop.
- To avoid executing specific parts of a loop under certain conditions.
JavaScript Labels
JavaScript labels allow you to name a block of code and use it with break
or continue
. This is especially useful when dealing with nested loops, as it lets you break out of or continue specific loops.
Syntax
javascript
1 2 3 4
labelName: { // Code block break labelName; }
Example: Breaking Out of a Labeled Block
Here’s how you can use a label to exit a code block:
javascript
1 2 3 4 5 6 7 8
outerBlock: { console.log("Start of block"); if (true) { break outerBlock; // Exit the labeled block } console.log("This will not be logged"); } console.log("End of block");
Output:
javascript
1 2
Start of block End of block
Example: Using Labels with Nested Loops
When working with nested loops, you can use labels with continue
to skip specific iterations of the outer loop:
javascript
1 2 3 4 5 6 7 8
outerLoop: for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { if (i === j) { continue outerLoop; // Skip the outer loop iteration when i equals j } console.log(`i = ${i}, j = ${j}`); } }
Output:
javascript
1 2 3 4 5
i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 3, j = 1 i = 3, j = 2
Key Points to Remember
break
: Stops the loop or code block entirely.- Use when you want to exit early from a loop or block.
continue
: Skips the current iteration and moves to the next one.- Use when you want to avoid specific iterations but continue the loop.
- Labels can be used to add flexibility when breaking out of or continuing nested loops or code blocks.