Conditional Statements
Instructions in JScript code execute sequentially by default. It might be useful, however, to alter the logical sequence and transfer control to a nonsequential part of the code depending on specific conditions. A control structure transfers program control to one of two places depending on whether a conditional statement tests true or false. Any expression coercible to a Boolean value can be used as a conditional statement. Some common condition statements are mentioned here.
Equality and Strict Equality
The equality operator (==) in a condition statement checks whether the two arguments passed to it have the same value, performing type conversion if necessary to make a comparison. The strict equality operator (===) compares both the value and type of two expressions; true is returned only if the value and data type are the same for the two operands. Note that the strict equality operator does not distinguish between the different numeric data types.
The following JScript code combines an equality operator with an if statement that uses it. For more information, see Control Structures.
function is2000(x) : String {
// Check if the value of x can be converted to 2000.
if (x == 2000) {
// Check is the value of x is strictly equal to 2000.
if(x === 2000)
print("The argument is number 2000.");
else
print("The argument can be converted to 2000.");
} else {
print("The argument is not 2000.");
}
}
// Check several values to see if they are 2000.
print("Check the number 2000.");
is2000(2000);
print("Check the string \"2000\".");
is2000("2000")
print("Check the number 2001.");
is2000(2001);
Following is the output of this code.
Check the number 2000.
The argument is number 2000.
Check the string "2000".
The argument can be converted to 2000.
Check the number 2001.
The argument is not 2000.
Inequality and Strict Inequality
The inequality operator (!=) returns the opposite result of the equality operator. If the operands have the same value, the inequality operator returns false; otherwise it returns true. Likewise, the strict inequality operator (!==) returns the opposite result of the strict equality operator.
Consider the following JScript code sample in which the inequality operator is used to control a while loop. For more information, see Control Structures.
var counter = 1;
// Loop over the print statement while counter is not equal to 5.
while (counter != 5) {
print(counter+);
}
The following is the output of this code.
1
2
3
4
Comparison
The equality and inequality operators are useful if a piece of data has a particular value. However, in some situations code may need to check if a value is within a particular range. The relational operators, less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=), are appropriate for these cases.
if(tempInCelsius < 0)
print("Water is frozen.")
else if(tempInCelsius > 100)
print("Water is vapor.");
else
print("Water is liquid.);
Short-Circuit
If you want to test several conditions together and you know that one is more likely to pass or fail than the others, you can use a feature called short-circuit evaluation to speed the execution of your script and avoid side effects that might cause errors. When JScript evaluates a logical expression, it evaluates only as many sub-expressions as are required to get a result.
The logical AND (&&) operator evaluates the left expression passed to it first. If that expression converts to false, then the result of the logical AND operator cannot be true regardless of the value of the right expression. Therefore, the right expression is not evaluated.
For example, in the expression ((x == 123) && (y == 42)), JScript first checks if x is 123. If it is not, the test for y is never made, and JScript returns the value false.
Similarly, the logical OR operator (||) evaluates the left expression first and if it converts to true, the right expression is not evaluated.
Short-circuiting is useful when the conditions to be tested involve the execution of function calls or other complex expressions. To make a script run most efficiently, place the conditions most likely to be true first for the logical OR operator. For the logical AND operator, place the conditions most likely to be false first.
An example of a benefit of designing your script in this manner is that runsecond() will not be executed in the following example if the return value of runfirst() converts to false.
if ((runfirst() == 0) || (runsecond() == 0)) {
// some code
}
Another example of a benefit of designing your script in this manner is that runsecond() will not be executed in the following example if the return value of runfirst() converts to false.
if ((x == 0) && (y/x == 5)) {
// some code
}
Other
Any expression that can be converted to a Boolean value can be used as a condition statement. For example, you could use an expression such as:
if (x = y + z) // This may not do what you expect - see below!
Note that the above code does not check if x is equal to y + z, since the syntax uses only a single equal sign (assignment). Instead, the code above assigns the value of y + z to the variable x, and then checks if the result of the entire expression (the value of x) can be converted to the value true. To check if x is equal to y + z, use the following code.
if (x == y + z) // This is different from the code above!