HTML and CSS Reference
In-Depth Information
6.3.5 Loop Control with break and continue
The control statements, break and continue , are used to either break out of a loop early
or return to the testing condition early; that is, before reaching the closing curly brace
of the block following the looping construct.
Table 6.1 Control Statements
Statement
What It Does
break
Exits the loop to the next statement after the closing curly brace of the
loop's statement block.
continue
Sends loop control directly to the top of the loop and re-evaluates the loop
condition. If the condition is true, enters the loop block.
EXAMPLE 6.8
<html>
<head>
<title>Looping Constructs</title>
</head>
<body>
1
<script type="text/javascript">
2
while(true) {
3
var grade=eval(prompt("What was your grade? ",""));
4
if (grade < 0 || grade > 100) {
alert("Illegal choice!");
5
continue;
// Go back to the top of the loop
}
if(grade > 89 && grade < 101)
6
{alert("Wow! You got an A!");}
7
else if (grade > 79 && grade < 90)
{alert("You got a B");}
else if (grade > 69 && grade < 80)
{alert("You got a C");}
else if (grade > 59 && grade < 70)
{alert("You got a D");}
8
else {alert("Study harder. You Failed.");}
9
answer=prompt("Do you want to enter another grade?","");
10
if(answer != "yes"){
11
break;
// Break out of the loop to line 12
}
12 }
</script>
</body>
</html>
 
 
Search WWH ::




Custom Search