TypeScript break Statement

Introduction

In this chapter, we will explore the break statement in TypeScript. The break statement is used to exit a loop or switch statement before it has completed its normal execution cycle. Understanding how to use the break statement is essential for controlling the flow of loops and switch cases in TypeScript programs.

Table of Contents

  • Definition
  • Break Statement Syntax
  • Using Break in Loops
  • Using Break in Switch Statements
  • Complete Example with Output
  • Conclusion

Definition

The break statement terminates the current loop, switch, or labeled statement, and transfers program control to the statement following the terminated statement.

Break Statement Syntax

Syntax

break;

Example

This example demonstrates using the break statement to exit a loop when a certain condition is met.

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break;
  }
  console.log(i);
}

Output

0
1
2
3
4

Using Break in Loops

The break statement can be used in any type of loop to exit the loop when a specified condition is met.

Example

This example uses the break statement to stop the loop when the value of num reaches 3.

let num: number = 0;

while (num < 10) {
  if (num === 3) {
    break;
  }
  console.log(num);
  num++;
}

Output

0
1
2

Using Break in Switch Statements

The break statement is commonly used in switch statements to terminate a case and prevent fall-through to subsequent cases.

Example

This example uses the break statement to execute a case block and then exit the switch statement.

let grade: string = 'B';
let result: string;

switch (grade) {
  case 'A':
    result = "Excellent";
    break;
  case 'B':
    result = "Good";
    break;
  case 'C':
    result = "Fair";
    break;
  case 'D':
    result = "Poor";
    break;
  default:
    result = "Unknown";
}

console.log(result); // Output: Good

Output

Good

Complete Example with Output

In this section, we will combine the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.

TypeScript Code

You can test the following code in the TypeScript Playground:

// Using Break in a For Loop
for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break; // Exit the loop when i equals 5
  }
  console.log(i); // Output: 0, 1, 2, 3, 4
}

// Using Break in a While Loop
let num: number = 0;

while (num < 10) {
  if (num === 3) {
    break; // Exit the loop when num equals 3
  }
  console.log(num); // Output: 0, 1, 2
  num++;
}

// Using Break in a Switch Statement
let grade: string = 'B';
let result: string;

switch (grade) {
  case 'A':
    result = "Excellent";
    break; // Exit the switch statement
  case 'B':
    result = "Good";
    break; // Exit the switch statement
  case 'C':
    result = "Fair";
    break; // Exit the switch statement
  case 'D':
    result = "Poor";
    break; // Exit the switch statement
  default:
    result = "Unknown";
}

console.log(result); // Output: Good

Conclusion

In this chapter, we covered the break statement in TypeScript, including how to use it in loops and switch statements. We provided a complete example with its output to illustrate how the break statement works in TypeScript. Understanding the break statement is essential for controlling the flow of loops and switch cases in TypeScript programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare