TypeScript control statements

TypeScript control statement is used to control the flow of program based on the specified condition.

TypeScript control statements:

1. If Statement
2. If else statement
3. if else if statement

TypeScript If Statement:

If statement is used to execute a block of statements if specified condition is true.

Syntax:

if(condition){
 //Block of TypeScript statements. 
}

TypeScript If Else Statement:

If else statement is used to execute either of two block of statements depends upon the condition. If condition is true then if block will execute otherwise else block will execute.

Syntax:

if(condition){ 
//Block of TypeScript statements1. 
}else{
 //Block of TypeScript statements2. 
} 

TypeScript If Else If Statement:

If else statement is used to execute one block of statements from many depends upon the condition. If condition1 is true then block of statements1 will be executed, else if condition2 is true block of statements2 is executed and so on. If no condition is true, then else block of statements will be executed.

Syntax:

if(condition1){
 //Block of TypeScript statements1. 
}else if(condition2){ 
//Block of TypeScript  statements2. 
} . . . else if(conditionn){
 //Block of TypeScript statementsn. 
}else{
 //Block of TypeScript statements. 
} 

TypeScript Control Statements Example:

var num:number = 2;  
if(num==1){  
console.log("TypeScript Statement 1");  
}  
else if(num==2){  
console.log("TypeScript Statement 2");  
}  
else if(num==3){  
console.log("TypeScript Statement 3");  
}  
else{  
console.log("TypeScript Statement n");  
}