Reader Level:
ARTICLE

Break Statement in TypeScript

Posted by Sharad Gupta Articles | TypeScript November 21, 2012
In this article I am going to explain how to use a break statement in TypeScript with an example.
  • 0
  • 0
  • 2606

Break Statement in TypeScript

The break statement can be used to influence the flow of execution during the execution of the loop statement, or say in other words the break statement can be used to terminate an iteration statement and will, when executed, cause the control flow to jump out to the next statement immediately following the iteration statement. In switch case statements, break causes the remaining cases to be skipped and it prevents "falling through" to other cases.

The Break statement is only used within loop and switch case statements.

Syntax

break;


The following example tells you, how to use a break statement in TypeScript, to do the following steps to create a program using break keywords.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window will be opened. Provide the name of your application like "breakStatement", thne click on the Ok button.


Step 2

After Step 1 your project has been created. The Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, css file and html files.
 

Step 3

The code of the break statement program is:

breakStatement.ts

 

class breakStatement {

 

BreakFunction()

 

{

 

for (var n = 1; n <= 5; n++) {

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

y.innerText = n.toString();

alert("hi" + n);

y.innerText = "Number= " + n + "\n";

document.body.appendChild(y);

if (n == 4)

{

break;

}

 

}

 

}

};

 

window.onload = () =>

{

var value = new breakStatement();

value.BreakFunction();
}


default.html
 

<!DOCTYPEhtml>

 

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

<head>

<metacharset="utf-8"/>

<title>TypeScript HTML App</title>

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

<scriptsrc="app.js"></script>

</head>

<body>

<h1>TypeScript HTML App</h1>

 

<divid="content"/>

</body>
</
html>


app.js

 

var breakStatement = (function () {

function breakStatement() { }

breakStatement.prototype.BreakFunction = function () {

for(var n = 1; n <= 5; n++) {

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

y.innerText = n.toString();

alert("hi" + n);

y.innerText = "Number= " + n + "\n";

document.body.appendChild(y);

if(n == 4) {

break;

}

}

};

return breakStatement;

})();

; ;

window.onload = function () {

var value = new breakStatement();

value.BreakFunction();

};

 

Step 4

Run your program and the output of the program is:


break-statement-in-typescript1.jpg

After clicking on the ok button:

break-statement-in-typescript2.jpg

The final output is:

break-statement-in-typescript.jpg

COMMENT USING

Trending up