Nested Loops, label and continue Statements


The label statement does not inherently go with the continue statement but, like discussing break with switch and case, you might find it useful to see the statements used in a mutual context. Likewise, nested loops typically are written without either label or continue statements, but they serve as a useful structure to help explain how to effectively use continue.

For the most part, I don't use continue because, like the break statement, it can signal sloppy programming practices and poor planning. However, when used appropriately and in the right context, continue can be a valuable programming option. The statement jumps out of sequence in a loop structure, but, unlike break, which exits the loop, continue jumps to test the termination condition of the loop, effectively skipping the current iteration of statements within the loop.

Consider a program in which a baseball team is sequentially given jersey numbers except for the numbers of specially recognized players whose numbers have been retired. Within a loop, the continue statement can jump to the beginning of the loop when any of the retired numbers are found in the loop. Furthermore, you have more than a single team, and the second team has the same number of players and uses the same jersey numbers. The first loop (outer) keeps track of the teams, and the second loop (inner) keeps track of the players and jerseys that they will be getting. When one loop resides inside another loop, it's called a nested loop.

In JavaScript, labels are not statements, but rather identifiers. If you have ever programmed in Basic, in which line numbers or labels are used to reference a line of code, you know what labels are. They are places in the script where the program can branch if a statement tells it to do so. The format for a label is as follows:

label: 
statements

In some respects, labels can be used like comments to help you organize your scripts, but they also can be used in conjunction with continue to send the program to execute the labeled portion of the script. Because the continue statement can be used only in loops, labeling the loops helps to control what the program will do. In the following script, the two loops are labeled team and jersey. Within the jersey loop is a conditional statement using continue that prevents the retired team numbers from being used. Note that the continue statement commands a jump to the beginning of the jersey loop, not the team loop. After you run the script, change the label next to continue from jersey to team.

<html> 
<head> 
<title>Using Continue and Labels</title> 
<script language="JavaScript"> 
var teamJ=""; 
var teamMember=0; 
team: 
     for(var outCount=1;outCount<3;outCount++) {
           jersey: 
                 for(var inCount=20;inCount<35;inCount++) {
                       if(inCount==22 || inCount==29 || inCount==30) {
                             continue jersey; 
                       } 
                 if (teamMember==12) {
                 teamMember=0; 
                 } 
                                    teamMember++; 
     teamJ += "Team" + outCount + "Member " + teamMember + " Jersey Number " + inCount + 
     "<br>"; 
     } 
} 
document.write(teamJ); 
</script> 
</head> 
<body bgColor="mediumspringgreen"> 
</body> 
</html>

The script output should look like the following:

Team1 Member 1 Jersey Number 20 
   Team1 Member 2 Jersey Number 21 
   Team1 Member 3 Jersey Number 23 
   Team1 Member 4 Jersey Number 24 
   Team1 Member 5 Jersey Number 25 
   Team1 Member 6 Jersey Number 26 
   Team1 Member 7 Jersey Number 27 
   Team1 Member 8 Jersey Number 28 
   Team1 Member 9 Jersey Number 31 
   Team1 Member 10 Jersey Number 32 
   Team1 Member 11 Jersey Number 33 
   Team1 Member 12 Jersey Number 34 
   Team2 Member 1 Jersey Number 20 
   Team2 Member 2 Jersey Number 21

It finishes with Member 12, and then starts over with Member 1.

Notice how all of the retired jersey numbers were omitted in the assignments for both teams. Now change this line:

continue jersey; 

to

continue team; 

When you run the program a second time, the output shows only the following four lines:

Team1 Member 1 Jersey Number 20 
   Team1 Member 2 Jersey Number 21 
   Team2 Member 3 Jersey Number 20 
   Team2 Member 4 Jersey Number 21

The reason that the second script produces only four lines in the browser window is that, as soon as the first retired number was detected, the program branched to the outer loop (team), incremented the value of the counter, and ended when the second reserved number was found because it had reached the termination condition. So, as you can see, depending on which label the continue statement branches to, very different outcomes are produced.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: George Freedrich at 10222008

Related Articles

1. Operators Precedence
The order in which expressions are evaluated based on their operators is known as precedence. Multiplication and division occur before addition and subtraction, so any op...

2. The ''with'' Statement
Like the ternary conditional operator, the with statement is a shortcut. Instead of having to list all of the properties of an object by repeating the basic objec...

3. Conditional Structures
The "thinking" structure in JavaScript is found in the different types of conditional statements in the language. Used in concert with different types of comparative oper...

4. Types of operators in JavaScript
Assignment Operators The key assignment operator is the equals sign (=). The left operand is a variable, an array element, or an objec...

5. JavaScript Lives in a Web Page
All the code that you write for JavaScript goes into an HTML page. If you don't know HTML yet, you should run out and get a good book on HTML. Lynda and William Weinman's...

6. JavaScript Literals
The raw data that make up the root of data types are called "literals." These are, in effect, literally what they represent themselves to be. Numbers, strings, and Boolea...

7. JavaScript Variables
I like to think of variables as containers on a container ship. You can put all different types of content into the containers, move them to another port, empty them, and...

8. JavaScript Operators
Operators can be placed into three categories-binary, unary, and ternary. Binary operators, most commonly associated with the concept of operator, take two (binary) expres...

9. JavaScript Arrays
Because objects are collections of properties with each property having its own name and value, arrays are actually JavaScript objects. Each property in an array is an el...

10. Loops in JavaScript
Loops in JavaScript are similar to loops in C++ and Java and most other languages using loop structures. In this section, you will find explanations of the different type...