1+ alert ( "Break & Continue in JS!" ) ;
2+ 3+ /*
4+
5+ JavaScript Break and Continue
6+ The break statement "jumps out" of a loop.
7+
8+ The continue statement "jumps over" one iteration in the loop.
9+
10+ The Break Statement
11+ You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch() statement.
12+
13+ The break statement can also be used to jump out of a loop:
14+
15+ Example
16+ for (let i = 0; i < 10; i++) {
17+ if (i === 3) { break; }
18+ text += "The number is " + i + "<br>";
19+ }
20+ In the example above, the break statement ends the loop ("breaks" the loop) when the loop counter (i) is 3.
21+
22+
23+ The Continue Statement
24+ The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
25+
26+ This example skips the value of 3:
27+
28+ Example
29+ for (let i = 0; i < 10; i++) {
30+ if (i === 3) { continue; }
31+ text += "The number is " + i + "<br>";
32+ }
33+
34+ */
35+ 36+ for ( let i = 0 ; i < 10 ; i ++ ) {
37+ 38+ if ( i === 5 || i === 3 ) {
39+ document . write ( "<br />Loop skip execution of Number " + i + " :: Because continue statement!" ) ;
40+ continue ; // When code come to this loop will not excute below any lines loop continue to go with next number of iteration
41+ }
42+ 43+ document . write ( "<br />Number: " + i ) ;
44+ if ( i === 7 ) {
45+ document . write ( "<br />Number is equal to 7 so loop breaked using break statement! π³ : LOOP NO LONGER EXECUTE!" ) ;
46+ break ;
47+ }
48+ }
49+ 50+ document . write ( "<br /><br />All links are now looped!" ) ;
0 commit comments