While Loop In TypeScript

Introduction

 
Today I am going to explain how to use a while loop in TypeScript. I give an example of using the while loop to find the reverse of any number. For example if I enter the number 1234 then it will give the result as 4321. The syntax of the while loop is as:
  1. Initialization;  
  2. While(Condition) {  
  3.  Increment / Decrement  
  4. }  
Now to use the while loop in TypeScript use the following.
 
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...".
 
Step 2
A window is shown. In it select HTML Application for TypeScript under the Visual C# template and give the name of your application that you want to give then click ok.
 
Step 3
In this application there are three files; they are app.ts (TypeScript file), app.js (JavaScript file) and the defult.htm (HTML file). Open the app.ts file.
 
Step 4
Write the following TypeScript code in the app.ts file:
  1. class Example_WhileLoop {  
  2.  constructor() {}  
  3.  Reverse() {  
  4.   var n: number;  
  5.   var s = 0;  
  6.   n = parseInt(prompt("Enter any Number"));  
  7.   var temp = n;  
  8.   while (temp != 0) {  
  9.    s = s * 10;  
  10.    s = s + temp % 10;  
  11.    temp = Math.floor(temp / 10);  
  12.   }  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "Reverse Number is->" + s;  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () => {  
  19.  var greeter = new Example_WhileLoop();  
  20.  greeter.Reverse();  
  21. };  
Step 5
Write the following code in the app.js file:
  1. var Example_WhileLoop = (function() {  
  2.  function Example_WhileLoop() {}  
  3.  Example_WhileLoop.prototype.Reverse = function() {  
  4.   var n;  
  5.   var s = 0;  
  6.   n = parseInt(prompt("Enter any Number"));  
  7.   var temp = n;  
  8.   while (temp != 0) {  
  9.    s = s * 10;  
  10.    s = s + temp % 10;  
  11.    temp = Math.floor(temp / 10);  
  12.   }  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "Reverse Number is->" + s;  
  15.   document.body.appendChild(span);  
  16.  };  
  17.  return Example_WhileLoop;  
  18. })();  
  19. window.onload = function() {  
  20.  var greeter = new Example_WhileLoop();  
  21.  greeter.Reverse();  
  22. };  
Step 6
Write the following code in the default.htm file:
  1. var Example_WhileLoop = (function() {  
  2.  function Example_WhileLoop() {}  
  3.  Example_WhileLoop.prototype.Reverse = function() {  
  4.   var n;  
  5.   var s = 0;  
  6.   n = parseInt(prompt("Enter any Number"));  
  7.   var temp = n;  
  8.   while (temp != 0) {  
  9.    s = s * 10;  
  10.    s = s + temp % 10;  
  11.    temp = Math.floor(temp / 10);  
  12.   }  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "Reverse Number is->" + s;  
  15.   document.body.appendChild(span);  
  16.  };  
  17.  return Example_WhileLoop;  
  18. })();  
  19. window.onload = function() {  
  20.  var greeter = new Example_WhileLoop();  
  21.  greeter.Reverse();  
  22. };  
Step 7
Now run the application and the output will be like:
 
While-loop-output-in-typescript.jpg
 
When I click the ok button then the result is displayed as:
 
Display-reverse-of-a-number-in-typescript.jpg
 

Summary

 
In this article, I explained how to use a while loop in TypeScript.


Similar Articles