Decision-making in a programming language parallels the decision-making processes encountered in everyday life. Within a programming language, the developer utilizes decision-making to define one or more conditions that the program will evaluate. This decision-making mechanism consistently yields a Boolean outcome of either 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
This represents a straightforward method of making decisions. It determines if the statements should be carried out or not; in other words, it evaluates the condition and returns true if the specified condition is met.
Syntax
if(condition) {
// code to be executed
}
Example
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 produces a result solely when the condition evaluates to true. However, if we wish to yield a result when the condition is false, we must employ the if-else statement. This structure assesses the condition; if it is true, the code within the if block is executed, while if it is false, the code in the else block is executed.
Syntax
if(condition) {
// code to be executed
} else {
// code to be executed
}
Example
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
In this scenario, a user can make a choice from various alternatives. The execution commences with a top-down methodology. Upon meeting a true condition, it executes the corresponding statement and skips the remaining conditions. If no conditions are evaluated as true, it proceeds to return the concluding else statement.
Syntax
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
}
Example
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
In this context, the if statement is directed at another if statement. A nested if statement refers to an if statement that exists within the body of a different if or else statement.
Syntax
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."
}
Example
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