72Part IIJavaScript TutorialA repeat looplets a script cycle (Make a web site)

72Part IIJavaScript TutorialA repeat looplets a script cycle through a sequence of statements until some condition ismet. For example, a JavaScript data validation routine might inspect every character that youenter into a form text field to make sure that each one is a number. Or if you have a collectionof data stored in a list, the loop can check whether an entered value is in that list. Once thatcondition is met, the script can then break out of the loop and continue with the next state- ment after the loop construction. The most common repeat loop construction used in JavaScript is called the forloop. It getsits name from the keyword that begins the construction. A forloop is a powerful devicebecause you can set it up to keep track of the number of times the loop repeats itself. The for- mal syntax of the forloop is as follows: for ([initial expression]; [condition]; [update expression]) { statement[s] inside loop} The square brackets mean that the item is optional. However, until you get to know the forloop better, I recommend designing your loops to utilize all three items inside the parenthe- ses. The initial expressionportion usually sets the starting value of a counter variable. Thecondition the same kind of condition you saw for ifconstructions defines the conditionthat forces the loop to stop going around and around. Finally, the update expressionis a state- ment that executes each time all of the statements nested inside the construction completerunning. A common implementation initializes a counting variable, i, increments the value of iby oneeach time through the loop, and repeats the loop until the value of iexceeds some maximumvalue, as in the following: for (var i = startValue; i <= maxValue; i++) { statement[s] inside loop} Placeholders startValueand maxValuerepresent any numeric values, including explicitnumbers or variables holding numbers. In the update expression is an operator you have notseen yet. The ++operator adds 1 to the value of ieach time the update expression runs atthe end of the loop. If startValueis 1, the value of iis 1 the first time through the loop, 2the second time through, and so on. Therefore, if maxValueis 10, the loop repeats itself 10times (in other words, as long as iis less than or equal to 10). Generally speaking, the state- ments inside the loop use the value of the counting variable in their execution. Later in thislesson, I show how the variable can play a key role in the statements inside a loop. At thesame time, you will see how to break out of a loop prematurely and why you may need to dothis in a script. FunctionsIn Chapter 5, you saw a preview of the JavaScript function. A functionis a definition of a set ofdeferred actions. Functions are invoked by event handlers or by statements elsewhere in thescript. Whenever possible, good functions are designed for reuse in other documents. Theycan become building blocks you use over and over again. If you have programmed before, you can see parallels between JavaScript functions and otherlanguages subroutines. But unlike some languages that distinguish between procedures(which carry out actions) and functions (which carry out actions and return values), only one
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services