Statements in Jscript are generally run in the write order. This kind of operation is called sequential operation, which is the default direction of the Program Stream.
Different from sequential running, another operation switches the program flow to another part of the script. That is, run another statement instead of the next statement in sequence.
To make the script available, the conversion of this control must be performed logically. The conversion controlled by the program is based on a "decision". The "decision" result is true or false (Boolean true or false is returned ). Create an expression and test its
No is true. There are two main program structures to implement this function.
The first option is to select the structure. It is used to specify the two program flows and create an intersection (like a fork) in the program ). There are four optional structures available in Jscript.
Single Selection structure (if ),
Two-way selection structure (if/else ),
Inline ternary operator? :
Multi-choice structure (switch ).
The second type of program control structure is a loop structure. Use the loop structure to specify the action to be repeated when some conditions are true. When the control statement conditions are met (usually after a specific number of iterations), the control skips the loop structure and passes it to the lower
Statement. There are four cycle structures available in Jscript.
Test the expression (while) at the beginning of the loop ),
Test the expression (do/while) at the end of the loop ),
Perform operations (for/in) on each attribute of an object ),
A loop (for) controlled by a counter ).
Through nesting, stack selection, and loop control structure, you can create rather complex scripts.
The third form of structured program flow is provided by accidental handling, which is not discussed in this document.
Conditional statements
JScript supports the if and if... else condition statements. Test A condition in the if statement. if the condition meets the test, execute the relevant JScript encoding. In the if... else statement, if the conditions do not meet the test conditions, different generations
. The simplest if statement format can be written in one row, but more common are the if and if... else statements of multiple rows.
The following example demonstrates various possible syntaxes using the if and if... else statements. The first example demonstrates the simplest Boolean test. When (and only if) the value of the item between the parentheses is (or can be forcibly converted to) true, if subsequent statements or languages
Statement.
// The smash () function is defined elsewhere in the code.
// Boolean test to check whether newShip is true.
If (newShip)
Smash (champagneBottle, bow );
// In this example, the test will not be met unless both conditions are true.
If (rind. color = "deep yellow" & rind. texture = "large and small wrinkles ")
{
TheResponse = ("Is it a Crenshaw melon? ");
}
// In this example, the test will satisfy any condition that is true.
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 Operators
JScript also supports Implicit Conditional format. This format uses a question mark (not the if before the condition) after the condition to be tested ). It also specifies two options, one used when the conditions are met, and the other used when the conditions are not met. These two options
Must be separated by a colon.
Var hours = "";
// The following Code specifies that the hours contains theHour content,
// It also contains theHour-12 content.
Hours + = (theHour> = 12 )? "PM": "AM ";
If you want to test multiple conditions together and know that a condition is more likely to meet or not meet the test than other conditions, you can use the feature called "Short Circuit computing" to speed up the running of the script. When JScript calculates the logical expression, only the result is obtained after calculation.
The child expression.
For example, if there is a "and" expression, such as (x = 123) & (y = 42), Jscript first checks whether x is 123. If not, the value of the entire expression cannot be true even if y is equal to 42. Therefore, y is not tested.
, Jscript returns false.
Similarly, if one of the multiple conditions is true (using the | Operator), the test stops when any of the conditions meets the test. This processing method is valid if the conditions to be tested include function calls or other composite expressions. For this reason
When writing an OR expression, write the condition most likely to be true. When writing an AND expression, first write the condition most likely to be false.
An example of the benefit of designing a script in this way is: In the following example, if runfirst () returns 0 or false, runsecond () is not run ().
If (runfirst () = 0) | (runsecond () = 0 )){
// Several codes.
}
Cycle
There are multiple ways to repeatedly execute a statement or statement block. Repeated execution is usually called loop or repetition. Repeat is just a loop operation. Typically, a variable test is used for control. The value of each cyclic variable is changed. JScript
Four types of loops are supported: for Loop, for... in loop, while loop, do... while loop.
Use for Loop
The for statement specifies a counter variable, a test condition, and the operation to update the counter. This condition is tested before each loop is repeated. If the test is successful, the code in the loop is run. If the test fails
The first line of code that follows the loop. After this loop is executed, the computer variable is updated before the next loop.
If the loop condition is never met, the loop is not executed. If the test conditions are always met, an infinite loop will occur. In some cases, the former may be necessary, while the latter is almost useless. Therefore, you must pay attention to it when writing loop conditions.
/*
The update expression ("icount ++" in the following example) will be executed at the end of the loop, that is, after the statement block that constitutes the loop body is executed, before the test condition.
*/
Var howFar = 10; // limit the number of cycles to 10.
Var sum = new Array (howFar); // create an Array named sum with 10 members, which ranges from 0 to 9.
Var theSum = 0;
Sum [0] = 0;
For (var icount = 0; icount TheSum + = icount;
Sum [icount] = theSum;
}
Var newSum = 0;
For (var icount = 0; icount> howFar; icount ++) {// This loop is not executed at all, because icount is not greater than howFar.
NewSum + = icount;
}
Var sum = 0;
For (var icount = 0; icount> = 0; icount ++) {// This is an infinite loop.
Sum + = icount;
}
Use the for... in Loop
JScript provides a special loop method to traverse all user-defined attributes of an object or all elements of an array. In the for... in loop, the cyclic counter is a string, not a number. It contains the name of the current attribute or
Subscript of the array element.
The following code example should be run in an Internet browser because it uses the alert method, which is not a Jscript.
// Create an object with certain attributes
Var myObject = new Object ();
MyObject. name = "James ";
MyObject. age = "22 ";
MyObject. phone = "555 1234 ";
// Enumerate all attributes of (loop) Objects
For (prop in myObject)
{
// Display "The property 'name' is James", and so on.
Window. alert ("The property" + prop + "'is" + myObject [prop]);
}
Although the for... in loop looks like the For Each... Next loop of VBScript, it is actually not the same. The for... in loop of JScript repeats all attributes of the Jscript object. VBScript's For Each... Next loop repeats the collection
All projects. To cycle all sets in JScript, you need to use the Enumerator object. Although some objects (such as those in Internet browsers) support the VBScript For Each... Next and Jscript for... in loops,
However, most objects are not supported.
Use a while loop
The while loop is similar to the for loop. The difference is that the while LOOP does not have a built-in counter or update expression. If you want to control the cyclic execution of statements or statement blocks, you need to not only "run the code n times", but more complex rules.
While loop. The following example uses the object model of the Internet browser and the while loop to ask users a simple question.
Var x = 0;
While (x! = 42) & (x! = Null ))
{
X = window. prompt ("What is my favorite number? ", X );
}
If (x = null)
Window. alert ("You gave up! ");
Else
Window. alert ("Yep-it's the Ultimate Answer! ");
Note that the while LOOP does not have an explicit built-in counter variable, so it is more likely to generate an infinite loop than other types of loops. In addition, since it is difficult to find out when and where the cyclic condition is updated, it is easy to write a condition that is not actually updated.
While loop. Therefore, be especially careful when writing a while loop.
As mentioned above, there is also do in JScript... the while loop is similar to the while loop. The difference is 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 preceding loop can be rewritten:
Var x = 0;
Do
{
X = window. prompt ("What is my favorite number? ", X );
} While (x! = 42) & (x! = Null ));
If (x = null)
Window. alert ("You gave up! ");
Else
Window. alert ("Yep-it's the Ultimate Answer! ");
Use the break and continue statements
In Microsoft Jscript, when certain conditions are met, the break statement is used to interrupt a loop. (Use the break statement to exit a switch block .). If it is a for or for... in loop
When the counter variable is used, use the continue statement to skip the remaining code block and directly jump to the next loop.
The following example uses the break and continue statements to control the Loop Based on the previous example.
Var x = 0;
Do
{
X = window. prompt ("What is my favorite number? ", X );
// Determine whether the user chooses to cancel the operation? If yes, exit the loop.
If (x = null)
Break;
// Do you want to enter a number?
// If yes, you do not need to enter a number.
If (Number (x) = x)
Continue;
// Only numbers are required.
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! ");