Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

Break and Continue Statements in TypeScript

Learn how to control loop execution using the break and continue statements in TypeScript.

Simple Explanation: The break statement stops a loop completely, whereas the continue statement skips the current iteration and proceeds to the next iteration.

What Will We Learn?

Sometimes, we need to stop a loop before it finishes or skip a particular iteration. TypeScript provides two statements for these situations.

  • Use break to terminate a loop immediately.
  • Use continue to skip the remaining statements in the current iteration.
  • Understand the difference between break and continue.
  • Apply these statements to practical programming examples.

Break and Continue Workflow

Understand how these statements affect the execution of a loop.

🔄
Step 1
Start Loop

Execute the loop statements.

→
🛑
Break
Stop Loop

Exit the loop immediately.

→
⏭️
Continue
Skip Iteration

Proceed to the next iteration.

1. Break Statement in TypeScript

The break statement immediately terminates the nearest enclosing loop. The program continues executing the statement after the loop.

Syntax

break;

Example

Print numbers from 1 to 10, but stop the loop when the number reaches 5.

for (let i: number = 1; i <= 10; i++) {
    if (i === 5) {
        break;
    }

    console.log(i);
}

console.log("Loop ended");

Expected Output

1
2
3
4
Loop ended

Explanation

  • The loop starts with i = 1.
  • Numbers from 1 to 4 are printed.
  • When i === 5, the break statement executes.
  • The loop terminates, and the next statement prints Loop ended.

2. Break Statement with While Loop

The break statement can also terminate a while loop when a specific condition is satisfied.

Example

let count: number = 1;

while (count <= 10) {
    if (count === 4) {
        break;
    }

    console.log(count);
    count++;
}

Expected Output

1
2
3

Explanation

When count becomes 4, the break statement terminates the loop before 4 is printed.

3. Continue Statement in TypeScript

The continue statement skips the remaining statements in the current iteration and moves to the next iteration of the loop.

Syntax

continue;

Example

Print numbers from 1 to 5, but skip the number 3.

for (let i: number = 1; i <= 5; i++) {
    if (i === 3) {
        continue;
    }

    console.log(i);
}

Expected Output

1
2
4
5

Explanation

  • The loop starts with 1.
  • When i === 3, the continue statement executes.
  • The console.log() statement is skipped for that iteration.
  • The loop continues with 4 and then 5.

4. Continue Statement with While Loop

When using continue in a while loop, make sure the loop variable is updated so that the loop can progress.

Example

let count: number = 0;

while (count < 5) {
    count++;

    if (count === 3) {
        continue;
    }

    console.log(count);
}

Expected Output

1
2
4
5

Explanation

The counter is incremented before the condition that uses continue. When the counter is 3, its output is skipped. The loop then continues with 4 and 5.

5. Difference Between Break and Continue

Break Continue
Terminates the nearest enclosing loop. Skips the current iteration.
No further iterations of that loop execute. The next iteration can execute.
Useful when a stopping condition is met. Useful when a particular iteration should be skipped.
break; continue;

6. Practical Example: Student Marks

Suppose we have an array of student marks. We want to skip a mark that is zero and stop processing when a mark of 100 is found.

Example

let marks: number[] = [75, 0, 82, 100, 90];

for (let i: number = 0; i < marks.length; i++) {
    if (marks[i] === 0) {
        continue;
    }

    if (marks[i] === 100) {
        break;
    }

    console.log("Marks:", marks[i]);
}

Expected Output

Marks: 75
Marks: 82

Explanation

  • The mark 75 is printed.
  • The mark 0 is skipped using continue.
  • The mark 82 is printed.
  • When the mark 100 is found, break stops the loop. The mark 90 is not processed.

Common Mistakes

  • Using break when only the current iteration needs to be skipped.
  • Using continue when the entire loop should stop.
  • Forgetting to update the counter in a while loop.
  • Placing the condition incorrectly inside the loop.
  • Forgetting that break and continue affect the nearest enclosing loop.
Remember: Use break to stop a loop and continue to skip an iteration.

Practice Task

Write TypeScript programs to complete the following tasks.

  • Print numbers from 1 to 10 and stop when the number reaches 7.
  • Print numbers from 1 to 10, skipping the number 5.
  • Display all positive numbers from an array using continue to skip zero and negative values.
  • Search for a particular value in an array and stop the loop when the value is found.
Challenge: Complete these examples using both for and while loops.

Summary

In this lesson, we learned how to use break to terminate a loop and continue to skip the current iteration. We also compared their behaviour, used them with for and while loops, and applied both statements in a practical student marks example.