TypeScript for loop tutorial

The for loop repeatedly executes a block of statements until a particular condition is true.
Syntax:

for(initialization; condition; statement){
 //Block of statements 
}

Where:
initialization statement: is used to initialize the loop variable.
boolean expression: is used for condition check whether returns true or false.
statement: is used for either increment or decrements the loop variable.

TypeScript for loop example:

for (num=1; num<=10; num++)  {  
console.log(num); 
}

Break Statement:

The break statement is a control statement which is used to jump out of the loop.
Syntax:

for( initialization; condition; statement){
  //Block of statements
  if(condition){ 
   break; 
  } 
}

Typescript for loop break example:

for (num=1; num<=10; num++)  {  
if(num==6){ 
   break; 
  } 
console.log(num);  
}

Continue statement

The continue statement is a control statement which is used to skip the following statement in the body of the loop and continue with the next iteration of the loop.
Syntax:

for( initialization; condition; statement){
  //Block of statements
  if(condition){ 
   continue; 
  } 
}

TypeScript for loop continue example:

for (num=1; num<=10; num++)  {  
if(num==6){ 
   continue; 
  } 
console.log(num);  
}

 

Please Share