JavaScript Introduction | Control Flow Statements | Other Control Flow Statements

Controlling Loops

In general, when a loop is entered through an expression check, every statement inside the loop is executed before the next expression is checked.
However, the continue and break statements let users directly control this normal loop flow.
Using a label statement can make the behavior of continue and break move the program flow to a specific area.

label Statement

A label statement is an identifier that lets you identify a specific area in a program. Using a label statement can make the behavior of continue and break move the program flow to a specific area.

The syntax of the label statement is as follows.

label:
specific area to identify

In the following example, the label arrIndex is used as an identifier that points to the entire for statement that follows it.

arrIndex:
for (var i in arr) {
    document.write(i);
}

continue Statement

The continue statement is used inside a loop to skip the rest of that loop and immediately move on to evaluating the next expression.
It is often used when you want to exclude processing for a specific condition inside an iteration statement.

In JavaScript, the continue statement can be used in the following two forms.

Syntax

1. continue;
2. continue labelName;

The following example prints integers from 1 to 100 except multiples of 3.

var exceptNum = 3;
for (var i = 0; i <= 100; i++) {
    if (i % exceptNum == 0) // Do not print multiples of exceptNum.
        continue;
    document.write(i + " ");
}

The following example uses a label to print only multiplication table values that are odd.

gugudan:
for (var i = 2; i <= 9; i++) {
    dan:
    for (var j = 1; j <= 9; j++) {
        if ((i*j) % 2 == 0)
            continue dan;
        document.write(i + " * " + j + " = " + (i*j) + "<br>");
    }
}

break Statement

The break statement is used inside a loop to completely terminate that iteration statement and move program flow to the statement immediately after it. In other words, it is used when you want to leave an iteration statement completely, regardless of the expression result inside the loop.

In JavaScript, the break statement can be used in the following two forms.

Syntax

1. break;
2. break labelName;

The following example prints the index that has a specific value in an array.

var lectures = ["html", "css", "JavaScript", "php"];
var topic = "JavaScript";
for (var i = 0; i < lectures.length; i++) {
    if (lectures[i] == topic) {
        document.write(topic + " is the " + (i + 1) + "th subject.");
        break; // After finding the desired value, exit without repeating the for statement any further.
    }
}

The following example uses a label to print the multiplication table only up to 3.

gugudan:
for (var i = 2; i <= 9; i++) {
    dan:
    for (var j = 1; j <= 9; j++) {
        if (i > 3)
            break gugudan;
        document.write(i + " * " + j + " = " + (i*j) + "<br>");
    }
}