Break and continue statements

Break and continue statements

In JavaScript programming, break and continue statements are used to control the flow of the default program loop in a JavaScript program. The break and continue statements in a program allow you to modify the default Behavior of the program loop, so that you can break the loop anywhere in your program and continue if needed and print the next statement. Where break and continue make your program flow flexible.

Break and continue statements

Break Statement in JavaScript.

In any JavaScript program, the break statement is used to break the default behaviour of the current program loop, as soon as you use the break statement keyword in a loop, it breaks the flow of the program loop. Once the break statement is executed in the program, the program loop stops, and the next statements after the loop in the program keep getting printed.

Break statement syntax.

break;

Example of break statement usage in a for loop.

for (let p = 1; p <=7; p++) {

if (p === 6) {

break; // here break statement break for loop at 6 when p equals 6

}

console.log(p);

}

Explanation of break above.

In this program, the loop starts from p = 1, and moves forward by 1 variable value each time.

When p reaches the number 6, the break statement is executed, and the for loop terminates immediately.

As a result, numeric values ​​from 1 to 5 are printed, but the loop does not move beyond 6.

Example of break statement usage in while loop.

let p = 1;

while (p < 11) {

if (p === 8) {

break; // here break statement break for loop at 8 number

}

console.log(p);

p++;

}

Explanation of break statement in while loop.

Here the for loop will print numeric values ​​from 1 to 7.

When the p variable in the while loop moves to the number 8, the break statement terminates the loop immediately.

continue Statement in JavaScript.

The continue statement in JavaScript programs is used to print the continue statement loop with the next repetition step in the current program by avoiding the current repetition step of the program loop. The continue statement does not terminate the current loop completely, whereas, the continue statement only breaks the given particular condition and prints the next statement, and continues the condition of the current program loop till the end.

Syntax of continue statement.

continue;

Example of continue statement usage in for Loop.

for (let p = 0; p < 9; p++) {

if (p === 7 ) {

continue; // here continue statement use to Skip the rest of the loop when p is equal to 7

}

console.log(p);

Explanation of continue statement.

Here the for loop starts from p = 0 and runs till p = 8.

When p moves to 7, the continue statement is executed which avoids the console.log(p) statement for the next repetition of the loop.

As a result, the number 7 is not printed but the continue statement continues the loop with the next iteration.

Example of use of continue statement in while Loop.

let p = 10;

while (p < 20) {

p++;

if (p === 12 || p === 14 || p === 17) {

continue; // here continue statement skip the rest of the loop when p is equal to 12, 14, or 17

}

console.log(p);

While loop explanation of continue statement.

The while loop continues as long as the value of p variable is less than 20.

When p variable moves to numeric value 12, 14 or 17, the continue statement executes the next statement, which causes the while loop to not print those numeric values.

As a result, the numeric values ​​12, 14 and 17 are not printed, but all other numeric values ​​are printed.

Whereas the break statement stops the program loop completely, the continue statement skips only the current given repetition, which causes the loop program to print the continue statement along with the continue statement.

Main Differences Between break and continue statement.

StatementKeyword PurposeBehaviour in program
breakBreak statement Exits the loop entirely, when it reaches given particular conditionBreak statement Stops the program loop and continues with the code after the loop reach
continueContinue statement use to Skips the current iteration of the loop, and print next statementContinue statement Jumps to the next iteration of the loop and print or check remaining statement

Example of use of break and continue statements together.

So let us demonstrate both break and continue statements in a JavaScript program by combining them in a loop.

for (let p = 0; p < 11; p++) {

if (p === 4) {

continue; // here continue statement Skip the 4 when if p equals 4

}

if (p === 8) {

break; // here break statement Exit the loop when if reach at p equals to 8

console.log(p);

}

Explanation of the above program.

In this the for loop runs from 0 to 10, but,

when the p variable moves to the number 4, the continue statement avoids that iteration and moves to the next statement.

When p moves to the numeric value 8, the break statement immediately exits the loop.

Uses of break and continue statements in JavaScript programming.

break statement.

If a particular condition is to be fulfilled in a JavaScript program, such as, finding the element or item in a group, which you were searching for in that program. Then you can use the break statement to exit that program loop quickly.

For example, searching for a specific element or value in an array and stopping the loop in the program when it is found, the break statement.

continue statement.

If a particular condition is fulfilled in a JavaScript program, then you can avoid loop repetition at that element and continue processing or printing the next statement of the loop.

For example, filtering some particular value in a JavaScript program. Where, invalid or unwanted data elements are skipped during loop repetition.

Conclusion of break and continue statements.

In any JavaScript program, the break and continue statements are used to control the default control flow of the loop.

Whereas the break statement allows the JavaScript programmer to exit the loop early when a particular condition is met.

The continue statement in a JavaScript program allows the programmer to avoid the current repetition step in the loop and continue with the next loop repetition step.

Leave a Reply