For and While Loops in TypeScript

like all other object-oriented programming languages in TypeScript also loop is used when need to executes a particular block for a numbers of time
There is mainly 3 types of loop and they are

  1. while loop
  2. do-while loop and
  3. for loop

While Loop

A while loop can be called an infinite loop also it can be break by using a certain condition or have to make the condition false as if the condition satisfied the loop will let enter inside it till the time the condition not gone to false

Example:

  var no:number = 5;
while(no >= 0){
console.log('the current value of number is : '+no);
no--;
}

Do while loop

It’s the same as while loop but it let enter into the loop for the first time if the condition is not satisfied also.

Example:

   var no1:number = 5;
do{
console.log('The current value of number is : ' + no1);
no1++;
}while(no1<10);

For loop

The for loop is a definite loop as its number of the loop is fixed and defined at the starting of the loop through the number of loops can be changed using the programming and break

Example:

    var no2:number = 5;
var i:number;
for(i = 0; i <= no2; i++){
console.log('The value of loop is : ' + i);
}

Share The Post On -

Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.