Learning JavaScript tutorial – JavaScript break, continue and Labels
JavaScript break statement
JavaScript break is a loop control statement. Break statements are commonly found inside the loop body; break helps us to control the execution of a loop. JavaScript Break statement (break keyword) may be used to break the flow and exits out of the loop structures of while loop, do-while loop, for loop, if condition and switch statement.
There are certain conditions when we need to break out of a loop before the complete loop gets executed. Or, in some cases we want to break out of the loop because there may be chances of an error. With the execution of the break statement, program flow will resume to the next sequential statement following the loop.
JavaScript break statement is an integral part of a switch case statement.
Break brings control out, as the statements in the switch case executed. If the break statement were omitted from switch case, the interpreter would execute each and every statement in all the switch case structures.
<script type="text/javascript" language="javascript" >
var x = prompt("Write a number in between 1 to 4");
switch (x)
{
case "1": alert(‘x is 1′);
break;
case "2": alert(‘x is 2′);
break;
case "3": alert(‘x is 3′);
break;
case "4": alert(‘x is 4′);
break;
default: alert(‘x is not 1, 2, 3 or 4′);
}
</script>
Read the rest of this entry »