Pre and Post Increment and Decrement: Difference between revisions
From Catglobe Wiki
More actions
Created page with "<span style="font-size:14px;">'''Pre and Post Increment and Decrement'''</span>  <span style="color:#000000;">++x is pre-increment and --x is pre-decrement</span> ..."  |
No edit summary  |
||
| (17 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
{{HelpFiles}} | |||
= Pre and Post Increment and Decrement = | |||
| <span style="color:#000000;">++x is pre-increment and --x is pre-decrement</span> | ||
<span style="color:#000000;">++ | <span style="color:#000000;">x++ is post-increment and x-- is post-decrement</span> Â | ||
<span style="color:#000000;"> | <span style="color:#000000;">With ++x and --x: means x is incremented '''BEFORE''' being used.</span> Â | ||
| <span style="color:#000000;">With x++ and x--: means x is incremented '''AFTER''' being used.</span> | ||
<span style="color:# | ==== <span style="color:#a52a2a;"><span style="font-size: 12px;">'''Examples'''</span></span>Â ==== | ||
< | <source lang="javascript">number a = 1; | ||
number b; | |||
b = ++a; | |||
print(b);Â Â Â Â Â Â Â // 2 | |||
print(a);Â Â Â Â Â Â Â // 2 | |||
b = --a; | |||
print(b);Â Â Â Â Â Â Â // 1 | |||
print(a); Â Â Â Â Â Â Â // 1</source> | |||
<source lang="javascript">number c = 3; | |||
 | number d; | ||
d = c++; | |||
 | print(d);         // 3 | ||
print(c); Â Â Â Â Â Â Â Â // 4 | |||
 | d = c--; | ||
print(d); Â Â Â Â Â Â Â Â // 4 | |||
 | print(c);         // 3</source>  | ||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
 | |||
[[Category:Operators]] | [[Category:Operators]] | ||
Latest revision as of 07:05, 14 December 2011
Pre and Post Increment and Decrement
++x is pre-increment and --x is pre-decrement
x++ is post-increment and x-- is post-decrement
With ++x and --x: Â means x is incremented BEFORE being used.
With x++ and x--: Â means x is incremented AFTER being used.
Examples
number a = 1;
number b;
b = ++a;
print(b); // 2
print(a); // 2
b = --a;
print(b); // 1
print(a); // 1
Â
number c = 3;
number d;
d = c++;
print(d); // 3
print(c); // 4
d = c--;
print(d); // 4
print(c); // 3