Inherited from v1.0.0

break keyword

break; exits the nearest enclosing while, for, for ... of, or switch. Bangla form: থামুন;.

Syntax

break;

Examples

Exit a loop early when a condition is hit:

for (var n of [1, 2, 3, 4, 5]) {
    if (n == 3) { break; }
    print(n);
}
// 1
// 2

Exit a switch case explicitly (rare — the case body already ends naturally):

switch (status) {
    case "ready": {
        do_work();
        break;          // optional; falling off the end has the same effect
    }
    default: { print("not ready"); }
}

Nested loops

break exits the innermost enclosing loop or switch only:

for (var i of [1, 2, 3]) {
    for (var j of [10, 20, 30]) {
        if (j == 20) { break; }   // exits the inner for-of, not the outer one
        print(i, j);
    }
}
// 1 10
// 2 10
// 3 10

For multi-level exits, use a sentinel variable in the outer loop's condition.

Bangla form

যতক্ষণ (i < 10) {
    যদি (i == 5) { থামুন; }
    print(i);
    i = i + 1;
}

See also