Table of contents
1.
Introduction
2.
Types of Loops
3.
For Loop
4.
While Loop
5.
Do While Loop
6.
Infinite Loop
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

TypeScript - Loops

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Typescript, the loop is a sequence of codes or instructions that are continuously repeated until a particular condition is satisfied. Generally, the loop consists of a counter which is checked over each iteration. If the condition is satisfied, the counter is either increased or decreased and the loop continues for the next counter. Otherwise, the loop is terminated.

Using the above flowchart, we have also shown the functionality of the Break Keyword. The break is a predefined termination keyword in Typescript often used to terminate loops and Switch in Typescript and other programming languages.

If a loop never satisfies the termination condition, it is referred to as an Infinite Loop.

Types of Loops

There are two types of Loops in Typescript.

  • Definite Loops: The type of loop where the number of iterations is fixed irrespective of values provided in the loop is known as a Definite Loop. The for Loop uses the concepts of Definite Looping.
  • Indefinite Loops: The type of loop where the number of iterations is not fixed and varies with the values provided is known as an Indefinite Loop. The while and do while Loop uses the concepts of Indefinite Looping.

We will discuss each of these looping techniques with their implementation in detail.

For Loop

Looping is generally achieved using either for, while, or do-while loops. In this section, we will be discussing the Typescript for looping.

The syntax of for loop looks like this,

for(Statement 1; Statement 2; Statement 3){   
//Codes or Instructions to be executed
}    

Statement 1: This statement is executed at the very beginning of the execution of the block. It performs the function of initializing the counter.

Statement 2: This statement contains the set of conditions to iterate over.

Statement 3: This statement is executed over each iteration and performs the function of increasing or decreasing the counter. 

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

The above code uses for loop to iterate over the first ten natural numbers and display their sum.

There is another variant of for loop known as the for-in loop. It is generally used to iterate over elements of an array or list or sometimes for characteristics in a string. The syntax of the for-in loop looks like this,

for(var in list){   
//Codes or Instructions to be executed
}

For Example,

var counter:any; 
var condition:any = "HeyNinja" //Declaring a variable of any datatype
for(counter in condition) {
   console.log(condition[counter])  
}

The above code uses the for-in loop to iterate over the variable “condition” and print each character in a separate line.  

While Loop

While loop uses the concepts of Indefinite Looping. Like for loop, while loop is an entry-based loop where the condition is checked at the beginning of code execution. The code block gets executed while the condition is true. The syntax of a while loop is,

while(condition){   
//Codes or Instructions to be executed
} 

Let us again take the example we considered above and find the sum of the first ten natural numbers but this time using the while loop.

var num: number = 10;
var counter: number = 1;
var sum: number = 0;
while(counter<=num){   
sum = sum+counter;
counter++;
}
console.log(sum);

Do While Loop

The do-while loop is very similar to the while as both use the concepts of indefinite looping, and the code block is executed while the condition is true. The do-while loop is an exit-based loop, where the condition is checked after executing the code. The syntax of a do-while loop is,

do{   
//Codes or Instructions to be executed
}
while(condition); 

For Example,

var counter: number = 1;
do{   
console.log("Iteration number: " +counter);
counter++;
}
while(counter < 4)

To explain what entry-level and exit-level is, let us take the following code and see the output for the while loop and for the do-while loop.

var counter: number = 4;
while(counter < 4){   
console.log("Iteration number: " +counter);
counter++;
}

The above code will not have any output as the condition is not satisfied for the first iteration, and the loop will terminate.

var counter: number = 4;
do{   
console.log("Iteration number: " +counter);
counter++;
}
while(counter < 4)

Contrary to that, the above code snippet uses an exit-level loop, and it will iterate once and then check for the condition. Thus we get this output.

Infinite Loop

As discussed earlier, loops that never terminate are known as infinite loops. An infinite loop can be implemented using any of the loops mentioned above. 

The flowchart clearly shows that Infinite loops don't have a termination block.

Let us see the implementation and syntax of each.

Using the for loop,

for( ; ; ){   
//Code to be executed indefinitely
}   

For Example,

for( ; ; ){   
console.log(“This is an infinite loop”);
} 

Using the while loop,

while(true){   
//Code to be executed indefinitely
}   

For Example,

while(true){   
console.log(“This is an infinite loop”);
} 

Using the do-while loop,

do{   
//Code to be executed indefinitely
}
while(true);  
For Example,
do{   
console.log(“This is an infinite loop”);
}
while(true);

Infinite loops can also occur due to some arithmetic exception or lousy logic.

FAQs

  1. How many loops are there in Typescript?
    There are two types of loops in Typescript, Definite and Indefinite loops. Definite loops further have the for loops under them, and the Indefinite loops have the while and do-while loops.
     
  2. Why are loops used?
    Loops are used to repeat a portion of code for a desired number of recursions without going through the hassle of typing the part multiple times.
     
  3. Difference between the break and continue statement?
    The Break Statement skips or terminates the entire loop block or a switch statement and continues with the rest of the code. In contrast, the Continue statement ignores the current iteration and executes the next iteration.

Key Takeaways

This Blog covered all the necessary points about looping in Typescript, discussing in-depth its functionality and methods of the appliance and explaining the difference between entry and exit level-based loops. We also discussed infinite loops in Typescript.

Recommended Reading: Characteristics of OOPS

Don't stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

 

Live masterclass