Loops in TypeScript

Loops are used to execute certain set of statements n number of times.

There are three types of loops in TypeScript: for loop, while loop and do-while loop.

for loop

for loop is used when we know the number of times we want to execute a set of statements.
Below is a sample code:

var i:number;
for(i=1; i<=10; i++){
    console.log(i);
}

Below is the output of above code:

while loop

while loop is used when we are not sure how many number of times we want to execute a set of statements.
Below is a sample code:

var num:number=30;
while(num>0 && num%3==0){
    console.log(num/3);
    num-=3;
}

Below is the output of above code:

do-while loop

do-while loop is just like while loop. The only difference is if the condition is false, while loop will not be executed even once, do-while will be executed at least once.
Below is a sample code:

var num:number=30;
do{
    console.log("Do-While loop will be executed at least once even if the condition is false!");
}while(num>40)

Below is the output of above code:

JavaScript Loops

Loops are basically used to iterate a piece of code multiple times. Loops are mostly used in arrays. There are 4 types of loops in JavaScript:

  • for loop
  • for-in loop
  • while loop
  • do-while loop

for loop

for loop is used to iterate a set of statements for a fixed number of times, that is, this loop can be used when number of iterations is used.

Syntax
for (initialization; condition; increment)
{
code to be executed
}

  • initialization: in this, we initialize our counter variable, This statement is executed only once before in the starting of the loop.
  • condition: This will test a condition for true or false. If this condition is true, the statements in the for loop are executed. If the condition becomes false, the statements in the for loop are not executed and control will come out of the loop.
  • increment: Here, we will increase or decrease our counter.

Below is the example of for loop:

document.write("For Loop!!&lt;br/&gt;");
for(i=1; i&lt;=5; i++)
{
document.write(i + "&lt;br/&gt;");
}

Below is the output of the above example:
For Loop!!
1
2
3
4
5

for-in loop

for-in loop is used to loop through the properties of an object or an array element. Here I have used in array in my example.

Below is the example of for-in loop:

document.write("For in loop!! &lt;br/&gt;");
var colors = ["Red", "Orange", "Green", "Blue", "White", "Black"];
var color;
for(color in colors)
{
document.write(colors[color] + "&lt;br/&gt;");
}

Below is the output of the above example:
For in loop!!
Red
Orange
Green
Blue
White
Black

while loop

while loop is used when you are not aware of the number of iteration of the loop.  This loop is executed until the condition in the while loop is true. Once the condition is false, the loop terminates and control will come out of the loop. while loops are almost similar to for loop, the difference is the number of known iterations.

Below is the example of while loop:

document.write("While Loop!! &lt;br/&gt;");
var i = 1;
while(i&lt;6)
{
document.write(i + "&lt;br/&gt;");
i++;
}

Below is the output of the above example:
While Loop!!
1
2
3
4
5

do-while loop

do-while loop is similar to while loop, the only difference is that the do-while loop is executed at least once and the condition is checked at the end of the loop.

Below is the example of while loop:

document.write("Do-While loop!! &lt;br/&gt;");
var i = 1;
do{
document.write(i + "&lt;br/&gt;");
i++;
}while(i&lt;6);

Below is the output of the above example:
Do-While loop!!
1
2
3
4
5

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started