In this article, we will learn about continue and break Statements in JavaScript with the help of examples.
Contents
JavaScript continue Statement
The continue statement skips the current iteration of the loop and directs the program’s control flow to the next iteration.
The continue statement has the following syntax:
continue [label]; |
Note: label is an optional feature that is rarely used.
Working of JavaScript continue Statement
continue with for Loop
Continue skips the current iteration in a for loop, and control flow jumps to the updateExpression.
Example 1: Print the Value of i using continue with for Loop
for (let i = 1; i <= 5; i++) { // condition to continue if (i == 3) { continue; } console.log(i); } |
Output:
1 2 4 5 |
The for loop is used in the above program to print the value of i after each iteration.
Notice the continue statement within the loop.
if (i == 3) { continue; } |
it means,
- The continue statement skips the third iteration when i is equal to 3.
- Then i is changed to 4, and the test condition and continue statement are re-evaluated.
- As a result, the numbers 4 and 5 are printed in the following two iterations.
Note: With decision-making statements, the continue statement is almost always used. Visit if-else Statements in JavaScript to learn more.
Visit for loop in JavaScript to learn more about for loop.
Note: The break statement ends the loop completely. The continue statement, on the other hand, skips only the current iteration.
continue with while Loop
Continue skips the current iteration and returns the program’s control flow to the while condition in a while loop.
The while and do…while loops both use the continue statement in the same way.
Example 2: Calculate a sum of Positive Number using continue with while Loop
// program to calculate positive numbers only // if the user enters a negative number, that number is skipped from calculation // negative number -> loop terminate // non-numeric character -> skip iteration let sum = 0; let number = 0; while (number >= 0) { // add all positive numbers sum += number; // take input from the user number = parseInt(prompt(‘Enter a number: ‘)); // continue condition if (isNaN(number)) { console.log(‘You entered a string.’); number = 0; // the value of number is made 0 again continue; } } // display the sum console.log(`The sum is ${sum}.`); |
Output:
Enter a number: 1 Enter a number: 2 Enter a number: hello You entered a string. Enter a number: 5 Enter a number: -2 The sum is 8. |
The user enters a number in the program above. The while loop is used to print the total sum of the user-inputted positive numbers.
Notice, the use of the continue statement.
if (isNaN(number)) { continue; } |
- The continue statement skips the current iteration if the user enters a non-numeric number/string. The program’s control flow then moves to the while loop condition.
- The loop ends when the user enters a number that is less than zero.
isNaN() is used in the above program to determine whether the value entered by a user is a number or not.
Visit while and do-while loop in JavaScript for more information on the while loop.
continue with Nested Loop
When continue is used between two nested loops, the current iteration of the inner loop is skipped.
For example:
// nested for loops // first loop for (let i = 1; i <= 3; i++) { // second loop for (let j = 1; j <= 3; j++) { if (j == 2) { continue; } console.log(`i = ${i}, j = ${j}`); } } |
Output:
i = 1, j = 1 i = 1, j = 3 i = 2, j = 1 i = 2, j = 3 i = 3, j = 1 i = 3, j = 3 |
When the continue statement is executed in the above program, it skips the current iteration in the inner loop and moves control flow to the inner loop’s updateExpression.
As a result, the value of j = 2 never appears in the output.
Labeled continue in JavaScript
When using nested loops, you can skip the current iteration and pass the program’s control flow to the updateExpression of a label statement.
In JavaScript, however, labeled continue is rarely used because it makes the code more difficult to read and understand.
JavaScript break Statement
When the break statement is encountered, the loop is terminated immediately.
The break statement has the following syntax:
break [label]; |
Note: label is optional and only used on rare occasions.
Working of JavaScript break Statement
break with for Loop
Example 1: program to print the value of i using break with for Loop
// program to print the value of i for (let i = 1; i <= 5; i++) { // break condition if (i == 3) { break; } console.log(i); } |
Output:
1 2 |
The for loop is used in the above program to print the value of i in each iteration. The break statement is used in the following way:
if (i == 3) { break; } |
This means that the break statement ends the loop when i equal 3. As a result, no values greater than or equal to 3 are included in the output.
Note: The break statement is almost always used with decision-making statements. Visit if-else Statements in JavaScript for more information.
Visit for loop in JavaScript to learn more about for loop.
break with while Loop
Example 2: program to find the sum of positive numbers using break with while Loop
// program to find the sum of positive numbers // if the user enters a negative numbers, break ends the loop // the negative number entered is not added to sum let sum = 0, number; while(true) { // take input again if the number is positive number = parseInt(prompt(‘Enter a number: ‘)); // break condition if(number < 0) { break; } // add all positive numbers sum += number; } // display the sum console.log(`The sum is ${sum}.`); |
Output:
Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: -5 The sum is 6. |
The user enters a number in the above program. The while loop is used to print the total sum of the user’s numbers.
The break statement is used as follows:
if(number < 0) { break; } |
When the user enters a negative number, such as -5, the break statement ends the loop, and the program’s control flow moves outside of it.
As a result, the while loop will keep running until the user enters a negative number.
Visit the while and do-while loop in the JavaScript page for more information.
break with Nested Loop
When a break statement is used between two nested loops, the inner loop is terminated.
For example:
// nested for loops // first loop for (let i = 1; i <= 3; i++) { // second loop for (let j = 1; j <= 3; j++) { if (i == 2) { break; } console.log(`i = ${i}, j = ${j}`); } } |
Output:
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 |
When i == 2 in the above program, the break statement is executed. It ends the inner loop and moves the program’s control flow to the outer loop.
As a result, the value of i = 2 never appears in the output.
Labeled break in JavaScript
When using nested loops, you can also use a label statement to end the outer loop.
In JavaScript, however, a labeled break is rarely used because it makes the code more difficult to read and understand.
That’s all about the continue and break statements in JavaScript.
Hope this article will guide you to recognize the continue and break statements in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.
Pingback: Switch Statement in JavaScript - Developers Dome
Pingback: JavaScript Hoisting - Developers Dome
Pingback: JavaScript Methods and this Keyword - Developers Dome
Pingback: JavaScript Prototype With Example - Developers Dome
Pingback: JavaScript Arrays With Example - Developers Dome
Pingback: JavaScript String With Example - Developers Dome
Pingback: JavaScript Number With Example - Developers Dome
Pingback: JavaScript Symbol with Example - Developers Dome
Pingback: JavaScript try-catch-finally Statement Example - Developers Dome