Nested Loops, label and continue Statements

written by: George Freedrich; article published: year 2008, month 10;

In: Root » Computers and technology » AJAX and JavaScript » Nested Loops, label and continue Statements

  Share  
|
  NL  |  FR  |  ES  |  PT  |  IT  |  DE  |  JP  |  CN  |  KR  |  RU  |  AE


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.

Share

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us or use the "Report this article" button on this page to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article