Wednesday, 21 March 2018

How to use break statement in JavaScript


The break statement allows us to break or exit a loop. Now here we create a Web page named BreakStatementUse.html which shows the use of break statement

##### Showing the Code for the BreakStatementUse.html file #######
<!DOCTYPE html>
<html>
<head>
<title>Using break Statement In javaScript</title>
</head>
<body background=”wall.jpg” text=”#ff98OO”>
<h1>Using break Statement in the while loop</h1>
<SCRIPT type=”text/javascript”>
var count=0;
while(count<10)
{
++count;
If((count%5==0))
break;
else
document.write(“count=”+count+”<br/>”);
}
document.write(“While loop exited”);
</SCRIPT>
</body>
</html>
Here if the value of the  count variable is less than 10 the while loop is executed. The if…else statement specified inside the while loop checks whether the value of the count variable is divisible by 2 or not. If the value of count variable is divisible by 2 then the break statement is executed. As a result the while loop is exited and the statement immediately following the while loop is executed.


#### Showing the output of the BreakStatementUse.html File. ######


No comments:

Post a Comment