11.1.2. Pre/Post Increment and Decrement
The ++ and -- operators are commonly used by programmers as shortcuts for incrementing and decrementing variables. The following three WMLScript statements are equivalent:
x = x + 1;
++x;
x++;
If the ++ operator is placed before a variable (e.g. ++x), we call this pre-increment; if the ++ operator is placed after a variable (e.g. x++), we call this post-increment. The following WMLScript examples can help you understand the difference between them:
var
x = 100;
var y = ++x;
After execution, x has the value 101 while y has the value 101. x is incremented first and then the result is assigned to y. The above block of code is equivalent to:
var
x = 100;
x = x + 1;
var y = x;
Now let's see an example of post-increment:
var
x = 100;
var y = x++;
After execution, x has the value 101 while y has the value 100. The initial value of x (i.e. 100) is assigned to y first and then x is incremented. The above block of code is equivalent to:
var
x = 100;
var y = x;
x = x + 1;
The -- operator is used in a similar way. If the -- operator is placed before a variable (e.g. --x), we call this pre-decrement; if the -- operator is placed after a variable (e.g. x--), we call this post-decrement. Below is an example of pre-decrement:
var
x = 100;
var y = --x;
After execution, x has the value 99 while y has the value 99, since x is decremented first before its value is assigned to y.
Below is an example of post-decrement:
var
x = 100;
var y = x--;
After execution, x has the value 99 while y has the value 100, since the initial value of x, i.e. 100, is assigned to y first. After that x is decremented.
Previous Page | Page 17 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data