Nested For Loop In TypeScript

Introduction

 
Nested For loop means "A For loop inside another For loop". The syntax is as follows:
  1. For(Initialization; Condition; Increment / Decrement) {  
  2.  For(Initialization; Condition; Increment / Decrement) {}  
  3. }  
When the condition of the first for loop is true, execution proceeds to the second loop and the second loop continues until the condition of the second loop becomes false then it will go to the first loop again and the process continues. To see how it works let's use the following steps.
 
Step 1
 
Open Visual Studio 2012 and click on "File" -> "New" -> "Project...".
 
Step 2
 
A window is opened. In it select HTML Application for TypeScript under Visual C# and then give the name of your application that you want to give and then click ok.
 
Step 3
 
The TypeScript file contains the app.ts file (TypeScript file) , app.js file (Javascript file ) and the default.htm file (HTML file). In this open the app.ts file.
 
Step 4
 
Write the following code in this app.ts file:
  1. class triangle {  
  2.  x: number;  
  3.  constructor(x: number, ) {  
  4.   document.getElementById("one");  
  5.   for (var i = 1; i <= 10; i++) {  
  6.    for (var j = 0; j <= i; j++) {  
  7.     var y = x * i;  
  8.     var span = document.createElement("span");  
  9.     span.innerText = "*";  
  10.     document.body.appendChild(span);  
  11.    }  
  12.    span.innerText = "\n";  
  13.    document.body.appendChild(span);  
  14.   }  
  15.  }  
  16. }  
  17. var p = new triangle(1);  
Step 5
 
Write the code in the app.js file as:
  1. var triangle = (function() {  
  2.  function triangle(x) {  
  3.   document.getElementById("one");  
  4.   for (var i = 1; i <= 10; i++) {  
  5.    for (var j = 0; j <= i; j++) {  
  6.     var y = x * i;  
  7.     var span = document.createElement("span");  
  8.     span.innerText = "*";  
  9.     document.body.appendChild(span);  
  10.    }  
  11.    span.innerText = "\n";  
  12.    document.body.appendChild(span);  
  13.   }  
  14.  }  
  15.  return triangle;  
  16. })();  
  17. var p = new triangle(1);  
Step 6
 
Write the following code in the default.htm file:
  1. < !DOCTYPEhtml >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > Example Of Print Triangle < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  /head>  
  17.  <  
  18.  body >  
  19.  <  
  20.  h1 id = "one" > Print Triangle < /h1>  
  21.  <  
  22.  div style = "color: #0094ff"  
  23. id = "content" >  
  24.  <  
  25.  script src = "app.js" > < /script>  
  26.  <  
  27.  /div>  
  28.  <  
  29.  /body>  
  30.  <  
  31.  /html>  
Step 7
 
Now run the application and the output will look like:
 
Nested-for-loop-in-typescript.jpg
 

Summary

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


Similar Articles