Nested Loops, label and continue Statements

by George Freedrich.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on ajax and javascript  

You are here: Categories » Computers and technology » AJAX and JavaScript

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.

Leave a comment or ask a question
Total comments: 0

AJAX and JavaScript Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
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 then replace the contain (more...)
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) expressions and combine them in (more...)
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 element, and each element (more...)
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 types of loops in JavaScript (more...)
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 object property, and the righ (more...)
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 operators, conditional statem (more...)
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 object, you can state the bulk (more...)
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 operands that are to be mul (more...)
Advantages and Disadvantages of AJAX Techniques ~ AJAX Without XMLHttpRequest - There are a number of cases in which you might not have XMLHttpRequest support. The most common would be in the case of an older browser. This is the hardest to work around, not because there (more...)
AJAX Usability Guidelines - Many usability experts have criticized AJAX by pointing out cases where it hurts usability. Although it is possible for AJAX to have that effect, I don't think AJAX inherently hurts usability; (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.