TypeScript Control Flow Statements

Introduction

In this chapter, we will explore control flow statements in TypeScript. Control flow statements allow you to control the execution of your code based on certain conditions and loops. Understanding these statements is essential for writing logical and efficient TypeScript programs.

Table of Contents

  • If Statement
  • If-Else Statement
  • Nested If Statement
  • Ternary Operator
  • Switch Statement
  • For Loop
  • While Loop
  • Do-While Loop
  • Break Statement
  • Continue Statement
  • Complete Example with Output
  • Conclusion

If Statement

The if statement executes a block of code if a specified condition is true.

Syntax

if (condition) {
  // code to be executed if the condition is true
}

Example

This example checks if the variable age is greater than or equal to 18 and prints a message accordingly.

let age: number = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

Output

You are an adult.

If-Else Statement

The if-else statement executes one block of code if a specified condition is true and another block of code if the condition is false.

Syntax

if (condition) {
  // code to be executed if the condition is true
} else {
  // code to be executed if the condition is false
}

Example

This example checks if the variable age is greater than or equal to 18 and prints a message accordingly. If the condition is false, it prints a different message.

let age: number = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

Output

You are a minor.

Nested If Statement

The nested if statement is an if statement inside another if or else block. It allows for multiple levels of condition checking.

Syntax

if (condition1) {
  // code to be executed if condition1 is true
  if (condition2) {
    // code to be executed if condition2 is true
  }
}

Example

This example checks if age is greater than or equal to 18 and further checks if age is greater than or equal to 21.

let age: number = 20;

if (age >= 18) {
  console.log("You are an adult.");
  if (age >= 21) {
    console.log("You can drink alcohol.");
  } else {
    console.log("You cannot drink alcohol.");
  }
}

Output

You are an adult.
You cannot drink alcohol.

Ternary Operator

The ternary operator is a shorthand for the if-else statement. It evaluates a condition and returns one value if true and another value if false.

Syntax

condition ? expressionIfTrue : expressionIfFalse

Example

This example checks if age is greater than or equal to 18 and assigns a message accordingly using the ternary operator.

let age: number = 18;
let message: string = age >= 18 ? "You are an adult." : "You are a minor.";
console.log(message);

Output

You are an adult.

Switch Statement

The switch statement evaluates an expression and executes code based on the matching case.

Syntax

switch (expression) {
  case value1:
    // code to be executed if expression === value1
    break;
  case value2:
    // code to be executed if expression === value2
    break;
  default:
    // code to be executed if expression doesn't match any case
}

Example

This example uses a switch statement to determine the name of the day based on the value of day.

let day: number = 3;
let dayName: string;

switch (day) {
  case 1:
    dayName = "Monday";
    break;
  case 2:
    dayName = "Tuesday";
    break;
  case 3:
    dayName = "Wednesday";
    break;
  default:
    dayName = "Unknown";
}

console.log(dayName); // Output: Wednesday

Output

Wednesday

For Loop

The for loop repeats a block of code a specified number of times.

Syntax

for (initialization; condition; increment) {
  // code to be executed
}

Example

This example prints the numbers from 0 to 4 using a for loop.

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

Output

0
1
2
3
4

While Loop

The while loop repeats a block of code as long as a specified condition is true.

Syntax

while (condition) {
  // code to be executed
}

Example

This example prints the numbers from 0 to 2 using a while loop.

let count: number = 0;

while (count < 3) {
  console.log(count);
  count++;
}

Output

0
1
2

Do-While Loop

The do-while loop repeats a block of code at least once, and then continues to repeat as long as a specified condition is true.

Syntax

do {
  // code to be executed
} while (condition);

Example

This example prints the numbers from 0 to 2 using a do-while loop.

let i: number = 0;

do {
  console.log(i);
  i++;
} while (i < 3);

Output

0
1
2

Break Statement

The break statement exits a loop or switch statement, terminating the current iteration.

Example

This example demonstrates using the break statement to exit a for loop when i equals 3.

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

Output

0
1
2

Continue Statement

The continue statement skips the rest of the loop iteration and proceeds with the next iteration.

Example

This example demonstrates using the continue statement to skip the iteration when i equals 3.

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

Output

0
1
2
4

Complete Example with Output

In this section, we will combine all 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:

// If Statement
let age: number = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

// If-Else Statement
age = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

// Nested If Statement
age = 20;

if (age >= 18) {
  console.log("You are an adult.");
  if (age >= 21) {
    console.log("You can drink alcohol.");
  } else {
    console.log("You cannot drink alcohol.");
  }
}

// Ternary Operator
age = 18;
let message: string = age >= 18 ? "You are an adult." : "You are a minor.";
console.log(message);

// Switch Statement
let day: number = 3;
let dayName: string;

switch (day) {
  case 1:
    dayName = "Monday";
    break;
  case 2:
    dayName = "Tuesday";
    break;
  case 3:
    dayName = "Wednesday";
    break;
  default:
    dayName = "Unknown";
}

console.log(dayName); // Output: Wednesday

// For Loop
for (let i = 0; i < 5; i++) {
  console.log(i);
}

// While Loop
let count: number = 0;

while (count < 3) {
  console.log(count);
  count++;
}

// Do-While Loop
let i: number = 0;

do {
  console.log(i);
  i++;
} while (i < 3);

// Break Statement
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}

// Continue Statement
for (let i = 0; i < 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}

Conclusion

In this chapter, we covered control flow statements in TypeScript, including the if statement, if-else statement, nested if statement, ternary operator, switch statement, for loop, while loop, do-while loop, and the break and continue statements.

We provided a complete example with its output to illustrate how these statements work in TypeScript. Understanding control flow statements is essential for writing logical and efficient 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