CD-50 Part II . JavaScript Tutorial (Web proxy server) About Repeat

CD-50 Part II . JavaScript Tutorial About Repeat Loops Repeat loops in real life generally mean the repetition of a series of steps until some condition is met, thus enabling you to break out of that loop. Such was the case earlier in this chapter when you looked through a bushel of tomatoes for the one that came closest to your ideal tomato. The same can be said for driving around the block in a crowded neighborhood until a parking space opens up. A repeat loop lets a script cycle through a sequence of statements until some condition is met. For example, a JavaScript data validation routine might inspect every character that you enter into a form text field to make sure that each one is a number. Or if you have a collection of data stored in a list, the loop can check whether an entered value is in that list. Once that condition is met, the script can then break out of the loop and continue with the next statement after the loop construction. The most common repeat loop construction used in JavaScript is called the for loop. It gets its name from the keyword that begins the construction. A forloop is a powerful device because you can set it up to keep track of the number of times the loop repeats itself. The formal 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 parentheses. The initial expression portion usually sets the starting value of a counter. The condition the same kind of condition you saw for if constructions defines the condition that forces the loop to stop going around and around. Finally, the update expression is a statement that executes each time all of the statements nested inside the construction complete running. A common implementation initializes a counting variable, i, increments the value of i by one each time through the loop, and repeats the loop until the value of i exceeds some maximum value, as in the following: for (var i = startValue; i <= maxValue; i++) { statement[s] inside loop } Placeholders startValue and maxValuerepresent any numeric values, including explicit numbers or variables holding numbers. In the update expression is an operator you have not seen yet. The ++operator adds 1 to the value of i each time the update expression runs at the end of the loop. If startValueis 1, the value of i is 1 the first time through the loop, 2 the second time through, and so on. Therefore, if maxValueis 10, the loop repeats itself 10 times (in other words, as long as iis less than or equal to 10). Generally speaking, the statements inside the loop use the value of the counting variable in their execution. Later in this lesson, I show how the variable can play a key role in the statements inside a loop. At the same time, you see how to break out of a loop prematurely and why you may need to do this in a script.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Leave a Reply