[X]

Javascript Break, Continue and With Statements - (Learning Javascript Series)

Javascript Break, Continue and With Statement

Break and continue statements are part of almost every programming language. They provides ability to stop/skip the execution in a program flow. These statements are only used inside a loop or switch statement. They are not valid outside of switch/loop statement.

Break statement is most frequently  used inside switch statement and is part of every case block. Whenever a break is encountered after a case, execution is immediately sent out of switch. Similarily when it is used inside a loop, once break is found, execution comes out of loop. Break statement says, "Exit the loop."

Example of Break statement:

 

<script type="text/javascript">
        //use of break statement in switch case
        var countryCodes = ["US", "UK", "IN", " KK  ", "?"];
        for (var cIndex in countryCodes) {
            switch (countryCodes[cIndex].replace(/^\s+|\s+$/g, "")) {
                case "UK":
                    document.write(countryCodes[cIndex] + " - " + "United Kingdom" + "<br />");
                    break;
                case "US":
                    document.write(countryCodes[cIndex] + " - " + "United Kingdom" + "<br />");
                    break;
                case "IN":
                    document.write(countryCodes[cIndex] + " - " + "United Kingdom" + "<br />");
                    break;
                case "", "?":
                default:
                    document.write(countryCodes[cIndex] + " - " + "Unknown Country Code" + "<br />");
            }           
        }
</script>


 

Continue statement is used to skip set of statements inside a loop perform further iteration with in the loop. Continue statement says, "Skip this iteration and move on to the next."

Example of Continue statement:

 

<script type="text/javascript">
        //use of continue statement inside loop
        //printing all the divisible numbers by 17 between 1 and 100
        for (var i = 1; i <= 100; i++) {
            if (i % 17 > 0) continue;
            document.write(i + " is divisible by 17.");
            document.write("<br />");
        }

</script>


 

With statement is used to set default object for next few line of statements so that we can avoid the repeatition of the same object over and over again. Suppose we have a "Country" object and there are associated attributes as behavior need to be invoked are assigned, instead of repeating "Country" object every time, we can set default object using "with" statement and using "." notation we can access/assign the attributes and behavior of this object inside the scope of with statement.

Example of with statement:

 

<script type="text/javascript">
        with (document) {
            title = "Javascript with statement.";
            write("This is line1.<br />");
            write("This is line2.<br />");
            write("This is line3.<br />");
        }
</script>

 

 

We saw how break and continue statements can be used to either exit from a loop and to skip the execution and move to next iteration inside loop or switch statement. Break and Continue can not be used outside of loop and switch. We also saw how with statement can be used to set default object and work with it.

blog comments powered by Disqus

Posts By Month