Javascript while break and continue


JavaScript break : ends the loop

The JavaScript break command indicates the end of the current loop, and then proceeds to execute the code after the loop. Break with the if conditional statement with the general use of the following examples:

var i=1
for (i = 1; i<=10; i++) {
    if (i > 5) {
        break;
    }
    document.write(i + "<br />");
}

operation result:

1
2
3
4
5

JavaScript continue : skips the current loop

Unlike the break command, JavaScript continues skipping the current loop (which is equivalent to an invalid loop) and continues the loop until the loop finishes.

The following example shows an odd number between output 1-10:

var i=1
for (i = 1; i<= 10; i++) {
    if ( i % 2 == 0 ) {
        continue;
    }
    document.write(i + "<br />");
}

operation result:

1
3
5
7
9
Note

As shown in the above example, regardless of whether break ends the loop or continue skips the current loop, note the location of the break / continue command. The above output odd number of examples, if the continue command on the document.write command, we can not achieve the results of our output odd.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook