TypeScript Loops

The loop is used to iterate and execute a group of statements under certain conditions. Loops provides reliability. When many times the same type of instruction and operation needs to be executed.

Loop Types

There are various loops available in type script programming. Which are used according to the situation. Such as while loop, for..loop and do..while loop etc.

While loops

While statement are used to execute looping statement under fixed number of times or it will targeting on a fixed condition which are control this loop execution.

var i : number = 1;
var table : number = 5;

// When i is less than 10
while(i <= 10)
{
    // Display table
    console.log(i ," X ",table," : ",  i * table)
    // Increase value
    i++;
}
1 ' X ' 5 ' : ' 5
2 ' X ' 5 ' : ' 10
3 ' X ' 5 ' : ' 15
4 ' X ' 5 ' : ' 20
5 ' X ' 5 ' : ' 25
6 ' X ' 5 ' : ' 30
7 ' X ' 5 ' : ' 35
8 ' X ' 5 ' : ' 40
9 ' X ' 5 ' : ' 45
10 ' X ' 5 ' : ' 50

For loop

for loop are similar to while-loop but syntactically there is different. It is based on three sections. The first initialization is the second test condition and the third is the updation process.

 for (initialization; condition; updation){
    // Loop instruction 
} 

Initialization It is used to declare variables. We can declare one or more than one variables in this section. Many times it is used to set initial value to variable which is already declared.

Condition This are used to declare variables. This are used to test boolean condition. This is main part which are control the loop process.

Updation This section are update the value of counter variables. For example.

let data: number[] = [10, 20 ,70, 50];

// For loop example
// Display array element
for(var index = 0; index < data.length; index++)
{
    // var index = 0 (Initialization)
    // index < data.length (Condition)
    // index++ (Updation)
    console.log(data[index]);
}
10
20
70
50

for...of Loop

for...of loop is used to get element of collection from start to last element.

let data: number[] = [10, 20 ,70, 50];
// Display array element
for(var value of data)
{
    console.log(value);
}
10
20
70
50

for...in Loop

for...in loop are used to iterate collection elements like array tuple and list. This loop are provide index position of given collection. We can access element by this position. For example.

let data: number[] = [10, 20 ,70, 50];
// Display array element
for(var index in data)
{
    console.log(data[index]);
}
10
20
70
50

Do while loop

do-while loops executes the loop statement given earlier, and checks the termination condition at the end of the loop. The main advantage of this loop is that, it will execute the loop statement at least once. When other termination conditions do not occur inside the loop.

var i : number = 10;

do
{
    // Enter loop

    // Display value of i
    console.log("A",i);

    // Update condition
    i += 10;

}while( i < 70); // Test Condition


do
{
    // Enter loop
    // Display value of i
    console.log("B",i);

    // Update condition
    i += 10;
    
}while( i < 10); // Test Condition


console.log("C",i);
A 10
A 20
A 30
A 40
A 50
A 60
B 70
C 80

Loop statements

Control statements of loop like break and continue they are change flow of loop executions. Break or Continue both are looping statement. This statement are used to controlling looping statement. Break statement are used to terminating loop execution. Continue statements are used to control back to the beginning of test condition.

continue Statement

The continue statement is used to change the execution follow of the loop. This statement are used in inside a loop in specified condition. When continue statement are executed they are not execute next remaining statement of loop body.

for loop continue statement

break Statement

break statement are used within loop an any place. With condition and without condition this statement can be used.

For-loop break statement


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved