Loop Structures in JScript
There are a lot of times when you would want to execute more or less the same statements for a large number of times. You can do this though the use of loops. The concept of looping is based on the fact that a value is changed every time the loop executes an iteration – which is a single execution of the loop. The loops is typically controlled by a test of a variable, and it is that variable's value that should change in the iteration, because if we don't do nothing, be will have an infinite loop which does the same thing over and over again, and it never stops. That's not good, and it may be the answer why your script hangs for a long time and it produces no result.
There are four types of loops in JScript: for loops, for…in loops, while loops, do…while loops. The easiest one is the for statement. It specifies a counter variable which it will use for a test condition, and an action that updates the counter before each iteration.
for (variable_initialization; expression; increment)
statements
"for" executes the statements as long as the specified expression is evaluated to "true". The following example fills an array with numbers from 0 to 9:
var numbers = new Array();
for (i = 0; i < 10; i++) //as long as the variable "i" is smaller than 10…
{
numbers[i] = i;
}
A special kind of loop structure is the for…in loop. Jscript uses it for stepping through all the elements of an array, or all the user-defined properties of an object. The syntax is slightly different from the original:
for (variable in [object | array])
statements
But if you want a more open approach to loops, you can always use the while structure. There is a slight difference between for loop and while loop – when you use while, you must do all the counter variable initialization and variation yourself. "while" will only accept a expression, and if the expression is evaluated to "true", then it will execute the statements you provide. Because while loops don't have an explicit built-in counter variable, they are more exposed to infinite loops, so always remember to change the counter-variable's value inside the loop! Here is the same loop example as before, but we use "while" instead of "for":
var numbers = new Array();
i = 0; //we have to do the initialization ourselves
while(i < 10)
{
numbers[i] = i;
i++; //don't forget to alter the counter-variable's value, or else you'll get an infinite loop
}
Similar to the while loop is the do…while loop, which actually performs the test after it executes the loop. This guarantees that the loop is executed at least once, after which it may or may not continue, based on the evaluation of the expression provided with while:
var numbers = new Array();
i = 0;
do
{
numbers[i] = i; //this is parsed at least once…
i++; //this too
}
while (i < 10);
There are times when you encounter special values within a loop, and you may want to simple exit the loop, or continue to the next iteration. JScript provides two such statements: "break" and "continue". The break statement immediately stops the execution of the loop, while the continue statement stop the current iteration only, and starts the next one. No matter which statement you choose to use, the previous statements before break or continue are always executed, but the following ones will not be parsed.
You can use break and continue with any of the "for", "while" or "do…while" loops.
var numbers = new Array();
for (i = 0; i < 10; i++) //as long as the variable "i" is smaller than 10…
{
if(i == 5)
continue; //let's skip the number 5
numbers[i] = i;
}
Other posts in asp
- What is ASP?
- Installing ASP
- First ASP Script
- JScript Syntax
- JScript Data Types
- Variables in JScript
- Operators & Expressions in ASP
- Arrays in JScript
- Conditional Structure in JScript
- Functions in JScript
- Object Oriented Programming in ASP
- Classes in ASP
- Response and Request in ASP
- File Manipulation in ASP
- Cookies in ASP
- Sessions in ASP
- Database Manipulation in ASP
- Debugging & Efficiency in ASP
- Beyond ASP
No comments yet. Be the first.
Leave a reply