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
1break;
Example: Exiting a Loop
In this example, the loop stops when the value of i
equals 5:
javascript
1let result = "";
2
3for (let i = 0; i < 10; i++) {
4 if (i === 5) {
5 break; // Exit the loop when i equals 5
6 }
7 result += "Counting: " + i + "\n";
8}
9
10console.log(result);
Output:
javascript
1Counting: 0
2Counting: 1
3Counting: 2
4Counting: 3
5Counting: 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
1continue;
Example: Skipping an Iteration
This example skips the iteration when i
is an even number:
javascript
1let output = "";
2
3for (let i = 0; i < 10; i++) {
4 if (i % 2 === 0) {
5 continue; // Skip even numbers
6 }
7 output += "Odd number: " + i + "\n";
8}
9
10console.log(output);
Output:
javascript
1Odd number: 1
2Odd number: 3
3Odd number: 5
4Odd number: 7
5Odd 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
1labelName: {
2 // Code block
3 break labelName;
4}
Example: Breaking Out of a Labeled Block
Here’s how you can use a label to exit a code block:
javascript
1outerBlock: {
2 console.log("Start of block");
3 if (true) {
4 break outerBlock; // Exit the labeled block
5 }
6 console.log("This will not be logged");
7}
8console.log("End of block");
Output:
javascript
1Start of block
2End 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
1outerLoop: for (let i = 1; i <= 3; i++) {
2 for (let j = 1; j <= 3; j++) {
3 if (i === j) {
4 continue outerLoop; // Skip the outer loop iteration when i equals j
5 }
6 console.log(`i = ${i}, j = ${j}`);
7 }
8}
Output:
javascript
1i = 1, j = 2
2i = 1, j = 3
3i = 2, j = 1
4i = 3, j = 1
5i = 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.