Learning JavaScript tutorial – JavaScript break, continue and Labels

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>

In the previous example, the value of x would determine which message was printed by comparing the value of the variable to the various case statements. If no match were found, the default statement would be executed. The break statement is also used commonly within switch to exit the statement once the appropriate choice is found. However, if we miss the break statement's use will execute all the statements in the switch cases. Just comment break in the previous and see the output.

JavaScript continue statement

JavaScript continue statement is also a loop control statement. continue statements are commonly found inside the loop body like break. The continue statement tells the interpreter to immediately start the next iteration of the loop. When it’s encountered, program flow will move to the loop check expression immediately.

Let's take a condition; we want to alert/print all the numbers in between 1 to 10 which are not dividable by 2.

<script type="text/javascript" language="javascript" >
var strDummy="";
for(i=1;i<=10;i++)
{
 if(i%2==0)continue;
strDummy+=i+ ", ";
}
alert("Number " + strDummy.substring(0, strDummy.length2) +" are not dividable by 2.");
</script>

In the above code we are going through a loop from 1 to 10. We are
checking whether each number is dividable by 2. If the result is yes then with the help of 'JavaScript continue statement' we start the next iteration of the loop. If the result is no then we are going to append that number in a dummy string strDummy and at last going to alert all the numbers in between 1 to 10 which are not dividable by 2.

JavaScript break Labels/continue Labels statement

A JavaScript labels are used with break and continue statement to redirect program flow more as per the programmers logic. A label is simply an identifier followed by a colon that is applied to a single statement or block of code.
Labels are optional parameter provided with break and continue. Without Labels both break and continue works in their natural way.

<script type="text/javascript" language="javascript" >
outerloop:
for (var i = 0; i < 3; i++)
{
  document.write("Outerloop: "+i+"<br />");
  for (var j = 0; j < 5; j++)
  {
    if (i == 1)
    break;
    if (j == 3)
    continue outerloop;
    document.write("Innerloop: "+j+"<br />");
   }
}
document.write("All loops done"+"<br />");
</script>

In the above code we have two for loops. First loops run three time; while in the second loop
var j = 0 reach to 3 continue outerloop; set the program flow Label 'outerloop:'. In second iteration when i=1 break take us out of the second loop. Program run till 1<3 and we get output as shown figure below.

JavaScript break, continue and Labels

JavaScript break, continue and Labels output

Leave a Reply

Your email address will not be published. Required fields are marked *