Program | control
Statements in Jscript scripts are typically run in the order in which they are written. This type of operation is called sequential operation and is the default direction of the program flow.
In contrast to sequential operation, another part of the program that runs the flow of programs to the script. That is, instead of running the next statement sequentially, you run a different statement.
For a script to be available, the transformation of the control must be performed logically. The conversion of a program control is based on a "decision", and the "decision" result is true or False (returns Boolean true or false). Create an expression, and then test whether it is true. There are mainly two kinds of program structure to realize this function.
The first is the choice of structure. Used to indicate the direction of two program flows, creating a crossover point (like a fork) in a program. There are four selection structures available in Jscript.
- Single selection structure (if),
- Two-way selection structure (if/else),
- Inline ternary operator ?:
- Multi-channel selection structure (switch).
The second type of program control structure is the loop structure. Use the loop structure to indicate the action to repeat when certain conditions remain true. When the condition of the control statement is satisfied (usually after certain numbers of iterations), the control skips the loop structure to the next statement. There are four types of looping structures available in Jscript.
- Test an expression (while) at thebeginning of the loop,
- Test the expression (do/while) at the end of the loop,
- Each property of an object is manipulated (for/in),
- Loop controlled by the counter (for).
By nesting and stack selection, loop control structures, you can create fairly complex scripts.
The third form of structured program flow is given by unexpected handling, which is not discussed in this document.
Working with conditional statements
JScript supports if and if...else conditional statements. A condition is tested in the if statement, and the associated JScript encoding is executed if the condition satisfies the test. In the if...else statement, different code is executed if the condition does not meet the test. The simplest if statement format can be written in one line, but more often than not, the if and if...else statements are multiple lines.
The following example shows the various possible syntax for using the if and if...else statements. The first example shows the simplest Boolean test. If the value of the item between (and only) parentheses is (or can be cast to) true , the subsequent statement or statement block ofif is executed.
The function is defined elsewhere in the code.
Boolean test to see
if it is true
. if (newShip)
In this example, the test will not be satisfied unless all two of the conditions are true. if (rind.color == "deep yellow " && rind.texture == "large and small wrinkles")
{
theResponse = ("Is it a Crenshaw melon?");
}
In this example, if any one of the conditions is true, the test is satisfied.var theReaction = "";
if ((dayOfWeek == "Saturday") || (dayOfWeek == "Sunday"))
{
theReaction = ("I'm off to the beach!");
}
else
{
theReaction = ("Hi ho, hi ho, it's off to work I go!");
}
Conditional operator
JScript also supports implicit conditional formatting. The format uses a question mark after the condition to be tested (not before the condition if ). It also specifies two options, one to use when the condition is met, and the other to use if the condition is not satisfied. The two selections must be separated by a colon.
The following code specifies whether the
content is included
or included
.hours += (theHour >= 12) ? " PM" : " AM";
If you want to test multiple conditions together, and you know that a condition is more likely to meet or not satisfy a test than other criteria, you can speed up the script by using an attribute called short-circuit calculation. When JScript evaluates a logical expression, only the child expressions needed to get the result are evaluated.
For example, if you have an "and" expression such as ((x = = 123) && (y = =), Jscript first checks to see if X is 123. If not, even if y equals 42, the value of the entire expression cannot be true. Therefore, Y is not tested and Jscript returns a value of false .
Similarly, if only one of the multiple conditions is true (use | | operator), the test stops when any one of the conditions satisfies the test. This is valid if the condition to be tested includes a function call or other compound expression. In this way, when you write an OR expression, you write a condition that is most likely to be true . When you write an and expression, you first write a condition that is most likely to be false .
An example of the benefits of designing a script in this way is that in the following example if runfirst () returns 0 or false, runsecond ()is not run.
if ((runfirst() == 0) || (runsecond() == 0)) {
Several code. }
Using loops
There are several ways to repeat a statement or block of statements. Often repeated executions are referred to as loops or repetitions . Repetition is just a run of loops. Typically, a variable test is used to control, and the value of each execution of the loop variable will change. JScript supports four kinds of loops : For Loop, for...in loop , while loop, do...while loop.
Use for loop
The For statement specifies a counter variable, a test condition, and an action to update the counter. The condition is tested before the repetition of each loop. If the test succeeds, the code in the loop is run. If the test is unsuccessful and the code in the loop is not shipped, the program continues to run the first line of code immediately following the loop. After the loop is executed, the computer variable is updated before the next loop.
If the loop condition is never satisfied, the loop is not executed. If the test condition is always satisfied, it will cause an infinite loop. In some cases, the former may be desirable and the latter is of little use, so be sure to note when compiling the cyclic conditions.
/*
The update expression
(in the following example "icount++"
) is executed at the end of the loop, after the block of statements that make up the loop body is executed, before the condition is tested. */
var howFar = 10;
Limit the number of loops to 10
. var sum = new Array(howFar);
creates an
array called and has
a member
from the
to 9
. var theSum = 0;
sum[0] = 0;
for(var icount = 0; icount < howFar; icount++) {
In this case, the count will be
counted from there
. theSum += icount;
sum[icount] = theSum;
}
var newSum = 0;
for(var icount = 0; icount > howFar; icount++) {
The loop will not be executed at all because it is not more
than howFar
. newSum += icount;
}
var sum = 0;
for(var icount = 0; icount >= 0; icount++) {
This is an infinite loop.sum += icount;
}
Using the for...in loop
JScript provides a special way to iterate through all the user-defined properties of an object or all the elements of an array. The loop counter in the for...in loop is a string, not a number. that contains the name of the current property or the subscript for the current array element.
The following code example should run in an Internet browser because it uses the alert method, which is not part of Jscript.
Creates a display of all the properties of an object var myObject = new Object();
myObject.name = "James";
myObject.age = "22";
myObject.phone = "555 1234";
Enumeration (loop) object with certain properties for (prop in myObject)
{
"The property 'name' is James"
, and so on. window.alert("The property '" + prop + "' is " + myObject[prop]);
}
Although the for...in Loop looks like VBScript's for each ... Next Loop, in fact, is not the same. The for...in loop of JScript repeats all the properties of a JScript object. For each of VBScript ... Next loops through all the items in the collection. To loop through all the collections in JScript, you need to use the enumerator object. Although some objects (like those in the Internet browser) support VBScript for each ... Next and Jscript for...in loops, but most objects are not supported.
Using the While loop
A while loop is similar to a for loop. The difference is that the while loop does not have a built-in counter or an update expression. If you want to control the loop execution of a statement or statement block, you need to not just "run the Code n times", but rather more complex rules with a while loop. The following example uses the Internet browser object model and the while loop to ask a user a simple question.
var x = 0;while ((x != 42) && (x != null)){ x = window.prompt("What is my favourite number?", x);}if (x == null) window.alert("You gave up!");else window.alert("Yep - it's the Ultimate Answer!");
Note because the while loop does not have an explicit built-in counter variable, it is easier to generate an infinite loop than other types of loops. In addition, because it is not easy to find out when and where the loop condition was updated, it is easy to write a while loop that actually never updates the condition. Therefore, you should be particularly cautious when writing a while loop.
As mentioned above, there is also a do...while loop in JScript that is similar to a while loop, except that it always runs at least once, because it checks the condition at the end of the loop, not at the beginning. For example, the above loop can be rewritten as:
var x = 0;do{ x = window.prompt("What is my favourite number?", x);} while ((x != 42) && (x != null));if (x == null) window.alert("You gave up!");else window.alert("Yep - it's the Ultimate Answer!");
Using break and Continue statements
In Microsoft Jscript, when certain conditions are met, a break statement is used to interrupt the operation of a loop. (Note that you also use the break statement to exit a switch block.) )。 If it is a for or for...in loop, use the continue statement to skip over the remaining blocks of code and jump directly into the next loop when the counter variable is updated.
The following example uses the break and continue statements to control loops based on the preceding example.
var x = 0;do{ x = window.prompt("What is my favourite number?", x);
Determine if the user chooses to cancel? If yes, exit the loop. if (x == null)
break;
Do you want to enter a number?
If so, you do not need to enter a number. if (Number(x) == x)
continue;
//
Require users to enter only numbers. window.alert("Please only enter in numbers!");
} while (x != 42)
if (x == null)
window.alert("You gave up!");
else
window.alert("Yep - it's the Ultimate Answer!");