You are reading the article Different Examples Of Typescript Loops updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Different Examples Of Typescript Loops
Introduction to TypeScript loopWhenever a block of code is to be executed multiple numbers of times, then we make use of loops in TypeScript. There are two kinds of loops in TypeScript, namely definite loop whose implementation is for loop and indefinite loop whose implementation is while loop and do. In contrast, loop, where a loop having a fixed or definite number of iterations is called a definite loop and a loop having indeterminate or the unknown number of iterations, is called an indefinite loop, and the while loop is executed as long as the condition is true, and do-while loop is similar to while loop except for the first time when the condition is not evaluated.
Start Your Free Software Development Course
There are two kinds of loops in TypeScript. They are:
definite loop
A loop that has a fixed or definite number of iterations is called a definite loop. The implementation of a definite loop is for a loop.
for loop
A code of block can be executed for a specified number of times using for loop. The syntax to declare for loop is as follows:
for (Statement1; Statement2; Statement3) { }where Statement1 specifies the initial count value to begin the iteration from,
Statement2 specifies the termination condition when the iteration is supposed to stop and
Statement3 specifies the number of steps by which the count is supposed to change.
Examples of TypeScript loopHere are the following examples mention below
Example #1TypeScript program to demonstrate the working of for loop using which the factorial of a given number is calculated and is displayed as a result on the screen:
var value:number = 10; var u:number; var fact = 1; fact = fact * u; } console.log("The factorial of the given value is:n") console.log(fact)The output of the above program is shown in the snapshot below:
indefinite loop
A loop having an indeterminate or unknown number of iterations is called an indefinite loop. The implementation of the indefinite loop is while loop and do while loop.
while loop
A code of block is executed as long as the condition is true. The syntax to declare while loop is as follows:
while (condition_statement) { }where condition_statement is the condition evaluated to true or false.
Example #2TypeScript program to demonstrate the working of while loop using which the factorial of a given number is calculated and is displayed as a result on the screen:
var value:number = 20; var fact = 1; { fact = fact * value; value--; } console.log("The factorial of the given value is:n"); console.log(fact);The output of the above program is shown in the snapshot below:
do-while loop
The do-while loop is similar to the while loop except for the first time when the condition is not evaluated. The syntax to declare while loop is as follows:
do{ } while (condition_statement)where condition_statement is the condition evaluated to true or false.
Example #3TypeScript program to demonstrate the working of do while loop using which numbers can be printed in the reverse order starting from a given value:
var value:number = 15; console.log("The numbers in reverse order starting from a given value is:n"); do { console.log(value); value--;The output of the above program is shown in the snapshot below:
In the above program, a variable of type number is defined and stored in value. Then do while loop is defined to print the starting number as it is followed by the other numbers printed in reverse order. Then the numbers in reverse order starting from a given value are displayed as the output on the screen.
Recommended ArticlesWe hope that this EDUCBA information on “TypeScript loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Different Examples Of Typescript Loops
Update the detailed information about Different Examples Of Typescript Loops on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!