EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript loop
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript loop

TypeScript loop

Introduction to TypeScript loop

Whenever 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.

There are two kinds of loops in TypeScript. They are:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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) {
//block of code
}

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 loop

Here are the following examples mention below

Example #1

TypeScript 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:

//a variable of type number is defined and stored in value
var value:number = 10;
//a variable of type number is defined and stored in u
var u:number;
//a variable of type number is defined and stored in fact
var fact = 1;
//for loop is defined to compute the factorial by decrement the given value stepwise by 1 until it is greater than or equal to 1
for(u = value;u>=1;u--) {
fact = fact * u;
}
//The factorial of the given value is displayed as the output on the screen
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:

TypeScript loop output 1

In the above program, a variable of type number is defined and stored in value. Then another variable of type number is defined and stored in u. Then another variable of type number is defined and stored in fact. Then a for loop is defined to compute the factorial by decrement the given value stepwise by 1 until it is greater than or equal to 1. Then the factorial of the given value is displayed as the output on the screen.

  • 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) {
//block of code
}

where condition_statement is the condition evaluated to true or false.

Example #2

TypeScript 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:

//a variable of type number is defined and stored in value
var value:number = 20;
//a variable of type number is defined and stored in fact
var fact = 1;
//while loop is defined to check the condition if the given value is greater than or equal to 1 and as long as the condition is true, the factorial is computed by decrementing the given value stepwise by 1
while(value>=1)
{
fact = fact * value;
value--;
}
//The factorial of the given value is displayed as the output on the screen
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:

TypeScript loop output 2

In the above program, a variable of type number is defined and stored in value. Then another variable of type number is defined and stored in fact. Then while loop is defined to check the condition if the given value is greater than or equal to 1 and as long as the condition is true, the factorial is computed by decrementing the given value stepwise by 1. Then the factorial of the given value is displayed as the output on the screen.

  • 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{
//block of code
} while (condition_statement)

where condition_statement is the condition evaluated to true or false.

Example #3

TypeScript program to demonstrate the working of do while loop using which numbers can be printed in the reverse order starting from a given value:

//a variable of type number is defined and stored in value
var value:number = 15;
//The numbers in reverse order starting from a given value is displayed as the output on the screen
console.log("The numbers in reverse order starting from a given value is:\n");
//do while loop is defined to print the starting number as it is followed by the other numbers printed in reverse order
do
{
console.log(value);
value--;
}while(value>=0);

The output of the above program is shown in the snapshot below:

output 3

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 Articles

We hope that this EDUCBA information on “TypeScript loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Operators
  2. Typescript Interview Questions
  3. TypeScript Versions
  4. What is TypeScript?
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more