![]() Capt. Horatio T.P. Webb | VBSCript and Javascript Parks -- Fall 2001 Last Updated 12PM 9/29/2001 |
The general form of the VBScript if is:
.
The <logical condition> must be either true or false (i.e., Boolean).
|
The general form of the Javascript if is:
{
The <logical condition> must be either true or false (i.e., Boolean).
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
These conditional tests can be created in several ways:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript Boolean variables
A VBScript variable may be assigned the values true and false using an assignment statement. Thus any variable (such as fred) can be assigned a logical value: fred=true or fred=false The variable can then be used in the program to change the logical flow of the program based on the true/false value of the variable. The general form above would then be:
Internally, false is stored as a value of zero. Any non-zero value is assumed to be true.
|
Javascript Boolean variables
A Javascript variable may be assigned the values true and false using an assignment statement. Thus any variable (such as fred) can be assigned a logical value: fred=true; or fred=false; The variable can then be used in the program to change the logical flow of the program based on the true/false value of the variable. The general form above would then be:
Internally, false is stored as a value of zero. Any non-zero value is assumed to be true.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Javascript if statements can have various layout styles that all work the same way. It is only a matter of style.
Consider the following alternatives:
or
or
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript relationals
Two variables can be compared to establish the true/false condition. These logical operators are:
These relational operators require two arguments. The general form of the relational is:
|
Javascript relationals
Two variables can be compared to establish the true/false condition. These logical operators are:
These relational operators require two arguments. The general form of the relational is:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Conditional operator
This is also called the "ternary" operator -- so called because it uses three expressions. The general form is: variable = ( expression1) ? (expression2) : (expression3) ; The first of the three expressions must always evaluate to "true" or "false", Following the question mark (?) are two expressions separated by a colon (:). If the first expression is "true", the second expression is executed; if the first expression is "false", the third expression is executed. This is equivalent to a simple if statement:
if ( logical expression) The conditional operator does NOT have to return a value from expression2) or expression3. These expressions may perform assignments themselves. Consider the case where expression2 and expression3 are strings: k = (g > 0) ? "g is positive" : "g is not positive"; Here if g is positive, k is assigned the string value "g is positive". if g is zero or negative, k is assigned the string value "g is not positive". Now consider this: (g > 0) ? k="g is positive" : k="g is not positive"; This produces exactly the same result as the first example.
The parentheses are optional and may be included only for readability. This conditional (ternary) operator thus allows you to place a simple if statement on a single line.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript Type Tests
Several test function can be used on a VbScript variable. They are
|
isNaN The function is used to test a string to determine if the string contains only numeric values. The IsNaN returns a true or false. For example: fred=isNan("12345"); returns a true to fred. fred=isNan("123KJH"); returns a false to fred. If a variable is assigned the value "NaN" then it will ALWAYS be false in an if statement. e.g.,
x=parseInt("blue"); // the variable x will be assigned the value "NaN"; The 2nd alert will always appear because "NaN" is always false. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript NOT (Negation)
The use of NOT reverses the sense of any logical expression. The general form is: NOT expression It is most often used with the if statement. Given the variable EOF: if EOF then (is true if EOF is true) but if NOT EOF then (is false if EOF is true).
|
Javascript ! (Negation)
The use of ! reverses the sense of any logical expression. The general form is: ! expression It is most often used with the if statement. Given the variable EOF: if ( EOF ) (is true if EOF is true) but if ( ! EOF ) (is false if EOF is true).
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript AND (Conjunction)
This allows two logical conditions to be tested simultaneously. The general form is: if condition 1 AND condition 2 then... The if statement is evaluated based on the true/false conditions as follows:
Note that BOTH conditons must be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.
|
Javascript && (Conjunction)
This allows two logical conditions to be tested simultaneously. The general form is: if ( condition 1 && condition 2 )... The if statement is evaluated based on the true/false conditions as follows:
Note that BOTH conditons must be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript OR (Disjunction)
This allows two logical conditions to be tested simultaneously. The general form is: if condition 1 OR condition 2 then... The if statement is evaluated based on the true/false conditions as follows:
Note that EITHER conditon can be true for the combined expressions to be true. Only BOTH conditions being false produces a false result.
|
Javascript || (Disjunction)
This allows two logical conditions to be tested simultaneously. The general form is: if (condition 1 || condition 2 )... The if statement is evaluated based on the true/false conditions as follows:
Note that EITHER conditon can be true for the combined expressions to be true. Only BOTH conditions being false produces a false result.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript XOR (Exclusive OR)
This allows two logical conditions to be tested simultaneously. The general form is: if condition 1 XOR condition 2 then... The if statement is evaluated based on the true/false conditions as follows:
Note that exactly ONE condition can be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.
This allows two logical conditions to be tested simultaneously. The general form is: if condition 1 EQV condition 2 then... The if statement is evaluated based on the true/false conditions as follows:
This allows two logical conditions to be tested simultaneously. The general form is: if condition 1 IMP condition 2 then... The if statement is evaluated based on the true/false conditions as follows:
|
Javascript XOR (Exclusive OR)
This allows two logical conditions to be tested simultaneously. The general form is: if condition 1 ^ condition 2 then... The if statement is evaluated based on the true/false conditions as follows:
Note that exactly ONE condition can be true for the combined expressions to be true. Otherwise the outcome is evaluated as false.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript One-Sided IF
The if statement can be used with only the statements to be executed when the true condiditon is encountered. the general form is:
|
Javascript One-Sided IF
The if statement can be used with only the statements to be executed when the true condiditon is encountered. the general form is:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript elseif
To test a sequence of conditions the shorthand form of the if statement uses the elseif form:
Note that there is only one end if that appears at the end of the elseif.
|
Javascript elseif
To test a sequence of conditions the shorthand form of the if statement uses the elseif form:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
VBScript Select Case
An alternative to using elseif is the Select Case. The general form is:
The use of the case else is optional. Note that the case expression may be a list ofm values separated by commas. For example:
Select Case fred
|
Javascript Switch An alternative to using elseif is the switch. The general form is:
The use of the default is optional. Note that the case expression may be a list. For example:
switch ( fred )
Note that multiple values for each case are NOT allowed. However, the task can be accomplished by using successive case statements prior to a single statement to be used for all the same outcome cases. |