Statements
Contents
Parent page: JScript
JScript Statements
This section of the JScript reference includes JScript:
Conditional Statements
Expressions and Operators
Conditional Statements
The main conditional statements supported by JScript are;
- if (expression) else
- else if
- switch
- while loop
- do while loop
- for loop
- for in
Care needs to be taken to code scripts that avoid infinite loops — that is, ensuring that the conditions will eventually be met.
The if.. else statement
The syntax is:
if Condition { } else if ANotherCondition { } else{ }
The switch statement
The switch
statement is like a multi-way branch. The basic syntax is:
switch (n) { case 1: n == 1 //execute code. break case 2: n==2 //execute code. break; default: // if all else fails... //execute code. break; }
The while statement
The while
statement is a basic statement that allows repetitive actions until a condition is met. It is possible that the statements inside the while
body never get executed if the condition is not met once.
while (expression) { //statement... }
The do...while statement
The do...while
statement is a variation on the while
statement, where the loop expression is tested at the bottom of the loop rather than at the top. This means the body of the loop is always executed at least once. The general syntax is:
do // statements while (expression);
The for statement
The for
statement provides a common loop statement with a counter variable of some kind. The syntax is:
for (initialize ; test ; increment) //statement
Example
for (count = 0; count < 10; count ++) showmessage(inttostr(count));
The for...in statement
The for...in
statement provides a way to loop through the properties of an object, or all the elements of an array. The for...in
loop does not specify the order in which the properties of an object are assigned to the variable.
for (variable in object) //statement.
Expressions and Operators
An expression is a valid combination of constants, variables, literal values, operators and function results. Expressions are used to determine the value to assign to a variable, to compute the parameter of a function or to test for a condition. Expressions can include function calls.
JScript has a number of logical, arithmetic, Boolean and relational operators. These operators are grouped by the order of precedence, which is different to the precedence orders used by Basic, C etc. For example, the AND and OR operators have precedence compared to the relational one.
Arithmetic Operators
| Addition |
| Subtraction |
| Multiplication |
| Division |
| Modulo |
| Decrement |
| Increment |
| Unary negation |
Logical Operators
| Logical Not |
| Less than |
| Greater than |
| Less than or equal to |
| Greater than or equal to |
| equality |
| inequality |
| Logical and |
| Logical or |
| conditional ternary |
| comma |
| strict equality |
| strict inequality |
Bitwise Operators
| Bitwise Not |
| Bitwise Left Shift |
| Bitwise Right Shift |
| Bitwise Unsigned right shift |
| Bitwise And |
| Bitwise Bitwise XOR |
| Bitwise OR |
Operator Precedence
Operator | Description |
---|---|
| Field access, array indexing, function calls, and expression grouping |
| Unary operators, return data type, object creation, undefined values |
| Multiplication, division, modulo division |
| Addition, subtraction, string concatenation |
| Bit shifting |
| Less than, less than or equal, greater than, greater than or equal, instanceof |
| Equality, inequality, strict equality, and strict inequality |
| Bitwise AND |
| Bitwise XOR |
| Bitwise OR |
| Logical AND |
| Logical OR |
| Conditional |
| Assignment, assignment with operation |
| Multiple evaluation |