while Loop in TypeScript
While loop is an also type of loop it will execute code until given condition is true. you code the keyword while followed by a condition expression in parentheses and a block of code in braces. when the while statement is executed, the conditional expression is evaluated. if the expression is true, the block of code is executed and the while loop is tried again. As soon as the expression evaluated to false, the block code is skipped and the while statement is done. if the expression evaluates to false the first time it is checked, the block of code won't be executed at all.
Syntax
|
while( initialize counter<condition)
{
increment a counter; |
The following example show the factorial of 5. In this example I have a loop class and define a while loop. we initialize a variable f=1. The while loop start position is 1 and continue to run as long as n is less than 5. f will increase by 1 each time the loop runs. Let's see how I implement while loop in TypeScript. Let's use the following steps.
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click HTML Application for TypeScript under Visual C#. Give the name of your application as "factorial" and then click ok.
Step 2
After this session the project has been created; A new window is opened on right side. this window is called the solution explorer. Solution explorer is contains the ts file, js file, css file and html file:
Coding
factorial.ts
|
class loop { fact: number; constructor (fact:number) { this.fact = fact; } factorial() { var count = 1; var f = 1; while (f<=this.fact) { count = count * f; f++ } alert("Factorial of 5 is ->" + count); } } window.onload = () => { var s = new loop(5); s.factorial(); }; |
factorialexample.html


Gowtham RajamanickamPosted Apr 12, 2016, 2:18 AM
Good One, Thanks for sharing