For Loop in TypeScript

For Loop in TypeScript

 
A for loop is classified as an iteration statement. The for loop is used when you know in advance how many times the script should run. To code a for statement, you code the keyword "for" followed by three statements in parentheses and a block of code in braces. The statements are separated by semicolons. The first statement in parentheses initializes the counter. The second statement is a conditional expression that causes the block of code to be executed as long as it's true. The third statement modifies the counter. It's executed after the block of code in the for loop is executed.
 
Syntax
  1. for (initialize a counter; conditional statement; increment a counter) {  
  2.  do this code;  
  3. }  
The following example shows the multiplication table of 5. In this example, I have a table class and define a loop in the class which starts with n=1. The loop will continue to run as long as n is less than 10. n will increase by 1 each time the loop runs. Let's use the following.
 
Step 1
 
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown as:
 
starting-window-type-script.gif
 
Give the name of your application as "table" and then click ok.
 
Step 2
  
After this session the project has been created; your new project should look like this. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, CSS file, and HTML file:
 
table-explorer-type-script.gif
 
Coding
 
super.ts
  1. class table {    
  2.  x: number;    
  3.  constructor(x: number, ) {    
  4.   for (var n = 1; n <= 10; n++) {    
  5.    var y = x * n;    
  6.    var span = document.createElement("span");    
  7.    span.innerText = x + "*" + n + "=" + y + "\n";    
  8.    document.body.appendChild(span);    
  9.   }    
  10.  }    
  11. }    
  12. var p = new table(5);    
superkeyworddemo.html
  1. < !DOCTYPEhtml >    
  2.  <    
  3.  htmllanghtmllang = "en"    
  4. xmlns = "http://www.w3.org/1999/xhtml" >    
  5.  <    
  6.  head >    
  7.  <    
  8.  metacharsetmetacharset = "utf-8" / >    
  9.  <    
  10.  title > Example Of Multiplication Table of 5 < /title>    
  11.  <    
  12.  linkrellinkrel = "stylesheet"    
  13. href = "app.css"    
  14. type = "text/css" / >    
  15.  <    
  16.  /head>    
  17.  <    
  18.  body >    
  19.  <    
  20.  h1 > Multiplication Table of 5 < /h1>    
  21.  <    
  22.  divstyledivstyle = "color:#0094ff"    
  23. id = "content" >    
  24.  <    
  25.  scriptsrcscriptsrc = "app.js" >    
  26.  <    
  27.  /script>    
  28.  <    
  29.  /div>    
  30.  <    
  31.  /body>    
  32.  <    
  33.  /html>    
app.js
  1. var table = (function() {    
  2.  function table(x) {    
  3.   for (var n = 1; n <= 10; n++) {    
  4.    var y = x * n;    
  5.    var span = document.createElement("span");    
  6.    span.innerText = x + "*" + n + "=" + y + "\n";    
  7.    document.body.appendChild(span);    
  8.   }    
  9.  }    
  10.  return table;    
  11. })();    
  12. var p = new table(5);     
Output
 
 final-result-table-type-script.gif
Reference By
http://www.typescriptlang.org/


Similar Articles