Decision Making

The decision making in a programming language is similar to decision making in real life. In a programming language, the programmer uses decision making for specifying one or more conditions to be evaluated by the program. The decision making always returns the Boolean result true or false.

There are various types of Decision making in TypeScript:

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

if statement

It is a simple form of decision making. It decides whether the statements will be executed or not, i.e., it checks the condition and returns true if the given condition is satisfied.

Syntax
if(condition) {
     // code to be executed
}
Decision Making
Example
snippet
let a = 10, b = 20;
if (a < b) 
{
    console.log('a is less than b.');
}
Output
a is less than b.

if-else statement

The if statement only returns the result when the condition is true. But if we want to returns something when the condition is false, then we need to use the if-else statement. The if-else statement tests the condition. If the condition is true, it executes if block and if the condition is false, it executes the else block.

Syntax
if(condition) {
  // code to be executed
} else {
   // code to be executed
}
Decision Making
Example
snippet
let n = 10
if (n > 0) { 
   console.log("The input value is positive Number: " +n);
} else {
    console.log("The input value is negative Number: " +n);
}
Output
The input value is positive Number: 10

if-else-if ladder

Here a user can take decision among multiple options. It starts execution in a top-down approach. When the condition gets true, it executes the associated statement, and the rest of the condition is bypassed. If it does not find any condition true, it returns the final else statement.

Syntax
snippet
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
else{
//code to be executed if all the conditions are false
}
Decision Making
Example
snippet
let marks = 95;
if(marks<50){
    console.log("fail");
}
else if(marks>=50 && marks<60){
    console.log("D grade");
}
else if(marks>=60 && marks<70){
    console.log("C grade");
}
else if(marks>=70 && marks<80){
    console.log("B grade");
}
else if(marks>=80 && marks<90){
    console.log("A grade");
}else if(marks>=90 && marks<100){
    console.log("A+ grade");
}else{
    console.log("Invalid!");
}
Output
A+ grade

Nested if statement

Here, the if statement targets another if statement. The nested if statement means if statement inside the body of another if or else statement.

Syntax
snippet
if(condition1) {
    //Nested if else inside the body of "if"
    if(condition2) {
       //Code inside the body of nested "if"
    }
    else {
       //Code inside the body of nested "else"
    }
}
else {
    //Code inside the body of "else."
}
Decision Making
Example
snippet
let n1 = 10, n2 = 22, n3 = 25  
if (n1 >= n2) {  
    if (n1 >= n3) {  
        console.log("The largest number is: " +n1)  
    }  
    else {  
        console.log("The largest number is: " +n3)  
    }  
}  
else {  
    if (n2 >= n3) {  
        console.log("The largest number is: " +n2)  
    }  
    else {  
        console.log("The largest number is: " +n3)  
    }  
}
Output
The largest number is: 25
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +