TypeScript – Decision Making


TypeScript – Decision Making


”;


In TypeScript, the decision making statements are used to control the flow of the execution based on certain conditions. TypeScript support all types of decision making constructs available in ES6, including if, if else, switch statements. As a superset of JavaScript, TypeScript inherits and expands upon JavaScript’s features including decision-making statements.

Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Shown below is the general form of a typical decision-making structure found in most of the programming languages −

Decision Making

A decision-making construct evaluates a condition before the instructions are executed. Decision-making constructs in TypeScript are classified as follows −

S.No. Statement & Description
1. if statement

An ‘if’ statement consists of a Boolean expression followed by one or more statements.

2. if…else statement

An ‘if’ statement can be followed by an optional ‘else’ statement, which executes when the Boolean expression is false.

3. else…if and nested if statements

You can use one ‘if’ or ‘else if’ statement inside another ‘if’ or ‘else if’ statement(s).

4. switch statement

A ‘switch’ statement allows a variable to be tested against a list of values.

Examples

Let”s understand the decision making in detail with the help of some examples in TypeScript.

Example: If Statement

In the example below, the if statement checks the condition “age >= 18”. The condition (boolean expression) is true so the statement within curly brackets ({}) is executed.

let age: number = 19;
if (age >= 18) {
    console.log("You care eligible for voting.");
}

On compiling it will generate the following JavaScript code.

let age = 19;
if (age >= 18) {
    console.log("You care eligible for voting.");
}

The output of the above example code is as follows –

You are eligible for voting.

So what if the condition (boolean expression) age >= 18 is evaluated as false.

If the condition is false, else statement will be executed.

Let’s check the flowing example –

Example: If…else statement

In this example, the condition (age >= 18) is evaluated to false so the statement following the else statement is executed.

let age: number = 17;
if (age >= 18) {
    console.log("You care eligible for voting.");
}
else {
    console.log("You are not eligible for voting.")
}

On compiling, it will generate the following JavaScript code.

let age = 17;
if (age >= 18) {
    console.log("You care eligible for voting.");
}
else {
    console.log("You are not eligible for voting.")
}

The output of the above example code is as follows –

You are not eligible for voting.

Example: Nested if statements

In the example below, else…if ladder used to check multiple conditions. The condition grade >= 80 evaluates to true so it will print “You got a B grade” in the console.

var grade: number = 85;
if (grade >= 90) {
  console.log("You got an A grade");
} else if (grade >= 80) {
  console.log("You got a B grade ");
} else if (grade >= 70) {
  console.log("You got a C grade ");
} else if (grade >= 60) {
  console.log("You got a D grade ");
} else {
  console.log("You got an F grade ");
}

On compiling, it will generate the following JavaScript code.

var grade = 85;
if (grade >= 90) {
  console.log("You got an A grade");
} else if (grade >= 80) {
  console.log("You got a B grade ");
} else if (grade >= 70) {
  console.log("You got a C grade ");
} else if (grade >= 60) {
  console.log("You got a D grade ");
} else {
  console.log("You got an F grade ");
}

The output of the above example code is as follows –

You got a B grade

Example: Switch statement

The example below, we use the grade variable as an expression of switch case statement. It verifies the value of grade against the case constants (A, B and C) and executes the corresponding block. If value of grade does not match with any case value, the default case will be executed.

var grade: string = ''B'';
switch (grade) {
    case ''A'': {
        console.log("Excellent");
        break;
    }
    case ''B'': {
        console.log("Good");
        break;
    }
    case ''C'': {
        console.log("Fair");
        break;
    }
    default: console.log("Unknown grade");
}

On compiling, it will generate the following JavaScript code.

var grade = ''B'';
switch (grade) {
    case ''A'': {
        console.log("Excellent");
        break;
    }
    case ''B'': {
        console.log("Good");
        break;
    }
    case ''C'': {
        console.log("Fair");
        break;
    }
    default: console.log("Unknown grade");
}

The output of the above example code is as follows –

Good

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *