ARTICLE

For Loop in TypeScript

Posted by Nitin Bhardwaj Articles | TypeScript October 12, 2012
In this article I have described how to use a for loop in TypeScript with an example.
Reader Level:

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

for ( initialize a counter; conditional statement; increment a counter)

{
  do this code;
 }

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

class table

{

    x: number;

    constructor(x:number, )

    {   

        for (var n = 1; n <= 10; n++)

        {         

            var y = x * n;

            var span = document.createElement("span"); 

             span.innerText = x+"*"+n+"=" + y+"\n"

             document.body.appendChild(span); 

        } 

    }

}

var p = new table(5);

 

superkeyworddemo.html

<!DOCTYPEhtml>

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>Example Of Multiplication Table of 5</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/> 

</head>

<body>

    <h1>Multiplication Table of 5</h1>

    <divstyle="color:#0094ff"id="content">

        <scriptsrc="app.js">

        </script>

    </div>

</body>

</html>

 

app.js

var table = (function ()

{

    function table(x)

    {

        for (var n = 1; n <= 10; n++)

        {

            var y = x * n;

            var span = document.createElement("span");

            span.innerText = x + "*" + n + "=" + y + "\n";

            document.body.appendChild(span);

        }

    }

    return table;

})();

var p = new table(5);

 

Output


 final-result-table-type-script.gif

Reference By

http://www.typescriptlang.org/

Login to add your contents and source code to this article
post comment
     

Thank u sir for clear my doubt...

Posted by Nitin Bhardwaj Oct 17, 2012

Isha, TypeScript has nothing to do with .NET. It is like JavaScript. You can write your code in any text editor like Notepad. IF you are asking what version if Visual Studio can you use to build TypeScript code, then looks like Visual Studio 2010 and 2012 support it.

Posted by Mahesh Chand Oct 16, 2012

Thank's Richa

Posted by Nitin Bhardwaj Oct 16, 2012

Very nice article and explanation Nitin

Posted by Richa Garg Oct 15, 2012

I want to know type script is supportive in all versions of .net or only in .net 4.5

Posted by isha goel Oct 15, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.