Programming Language
TypeScript
Conditional Statements
Decision Making Statements

Decision-Making Statements in TypeScript

Introduction

In programming, decision-making allows a program to take different actions based on certain conditions. It helps a program decide what to do next, depending on whether certain conditions are true or false.

Types of Decision-Making Statements in TypeScript:

  1. If Statement
  2. If-else Statement
  3. If-else-if Ladder
  4. Nested If
  5. Switch Statement
  6. Ternary Operator

1. If Statement

The if statement checks if a condition is true. If it is true, the code inside the if block runs. If not, the program does nothing.

Syntax:

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

Example:

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

2. If-else Statement

The if-else statement works like the if statement but adds an else part that runs when the condition is false.

Syntax:

if (condition) {
  // Code if condition is true
} else {
  // Code if condition is false
}

Example:

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

3. If-else-if Ladder

The if-else-if ladder is used when we have multiple conditions to check. The program checks each condition one by one. If one condition is true, the corresponding code is executed. If none of the conditions are true, the final else block is executed.

Syntax:

if (condition1) {
  // Code if condition1 is true
} else if (condition2) {
  // Code if condition2 is true
} else {
  // Code if all conditions are false
}

Example:

let score = 85;
 
if (score >= 90) {
  console.log("Grade A");
} else if (score >= 80) {
  console.log("Grade B");
} else if (score >= 70) {
  console.log("Grade C");
} else {
  console.log("Grade D");
}

4. Nested If

A nested if is when you place an if statement inside another if statement. It allows checking multiple conditions in a step-by-step manner.

Syntax:

if (condition1) {
  if (condition2) {
    // Code if both conditions are true
  }
}

Example:

let age = 18;
let hasLicense = true;
 
if (age >= 18) {
  if (hasLicense) {
    console.log("You can drive.");
  } else {
    console.log("You need a driving license.");
  }
} else {
  console.log("You are too young to drive.");
}

5. Switch Statement

The switch statement is used to check multiple values of one variable. It’s an alternative to using many if-else statements when comparing the same variable to different values.

Syntax:

switch (expression) {
  case value1:
    // Code if expression equals value1
    break;
  case value2:
    // Code if expression equals value2
    break;
  default:
    // Code if no cases match
}

Example:

let day = 3;
 
switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Invalid day");
}

6. Ternary Operator

The ternary operator is a shorthand for the if-else statement. It checks a condition and returns one of two values based on whether the condition is true or false.

Syntax:

condition ? value_if_true : value_if_false;

Example:

let age = 20;
let result = age >= 18 ? "Adult" : "Minor";
console.log(result);  // Output: Adult

This document explains the basic decision-making statements in TypeScript in simple terms. You can use these statements to control the flow of your program based on conditions.