Break and Continue concept in JavaScript


Break and continue are two very well known keywords since past two decades but yet these are a trouble for new programmers, so in this article we cover the break and continue statements with example. Break is used to break the block and continue is used to skip and move back to start of the block.

Break and Continue are very powerful construct. They appeared in C and since then they are following the programming world. Yes, they are old horse in this race. Break and continue, in most cases confuse the developers. We have met many programmers, who are learning stage and are mostly puzzled with the break and continue keywords and their working. So in this article we age going to make it simple for all of them.

Break is a keyword which is used to break the execution of a block and the control is transferred at the end of that block. Most commonly break is used in the breaking of loops, and continue is a little bit different however the scope of the continue is same as break. The continue is used to skip the execution of the code block and move back to the top of the block again. That is to say if the execution encounters the continue in the program it will skip the rest of code in that block and move back to next iteration of the loop.

In this article we are going to show the example of both the break and continue statements.

Break

break(all small latter) keyword is used in general case to break the execution of a code block and move the control to the end of the code block. Most common example I can give you is the switch-case construct, there is a break statement after the each case statement, so that after the matching and execution of one case the program flow move to the end of the switch block leaving the execution of rest of case statements, if we do not apply break there all the statements would be executed for example.


i = 1;
switch(i){
	case 1:
		console.log("case 1");
		break;
	case 2:
		console.log("case 2");
		break;
	case 3:
		console.log("case 3");
		break;
	default:
		console.log("default case");
		break;
}

Output:
case 1

this code example was perfect with all the break statements, now let us see what happens when we don't use the break.


i = 1;
switch(i){
	case 1:
		console.log("case 1");
	case 2:
		console.log("case 2");
	case 3:
		console.log("case 3");
	default:
		console.log("default case");
}

Output:
case 1
case 2
case 3
default case

So did you see the effect on not placing break, we expect the control to exit the switch when it got the case but no, it goes through all the cases. Similarly we will show you another example with a for loop.


for(i=0;i<10;i++){
	if(i==5){
		break;
	}
	console.log(i);
}

Output:
0
1
2
3
4

What Happens in this case is, every time the loop iterate it checks for the value of i, if it is equal to 5, this will break the loop and it will not iterate further. So there you are, it is as simple as that. Now it is time to look at the continue statement also.

Continue

the continue(all small latter) is used to skip the block of code and move to next iteration, this is used in case of loops only. Well, as far as my professional experience matters I have never seen the continue statement anywhere else except for the loop construct. So here we present a very simple example of using the continue statement.


for(i=1;i<=10;i++){
	if(i%2==0){
		continue;
	}
	console.log(i);
}

Output:
1
3
5
7
9

In this case also we haven't placed the else statement in case you think if it's not if its going to be else, so if its not if its not else but it will proceed further, but to show that the control actually does not move to next statement we have not used else. So as you see the control flow of the program, in case of even number which are satisfied by the condition x%2==0; where x is any positive integer, the loop will not execute console output statement.