Difference between revisions of "Pre and Post Increment and Decrement"

From Catglobe Wiki
Jump to: navigation, search
Line 19: Line 19:
 
<br>  
 
<br>  
  
&nbsp;<source lang="c">number a = 1;
+
&nbsp;<source lang="javascript">number a = 1;
 
number b;
 
number b;
 
b = ++a;
 
b = ++a;
Line 28: Line 28:
 
print(a);              // 1</source>&nbsp;  
 
print(a);              // 1</source>&nbsp;  
  
&nbsp;  
+
&nbsp;<source lang="javascript">number c = 3;
 
+
number d;
<span style="color:#000000;">number c = 3;</span>
+
d = c++;
 
+
print(d);                 // 3
<span style="color:#000000;">number d;</span>
+
print(c);                 // 4
 
+
d = c--;
<span style="color:#000000;">d = c++;</span>
+
print(d);                 // 4
 
+
print(c);                 // 3</source>
<span style="color:#000000;">print(d);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span><span style="color:#008000;">// 3</span>
 
 
 
<span style="color:#000000;">print(c); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span><span style="color:#008000;">// 4</span>
 
 
 
<span style="color:#000000;">d = c--;</span>
 
 
 
<span style="color:#000000;">print(d);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</span><span style="color:#008000;">// 4</span>
 
 
 
<span style="color:#000000;">print(c);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#008000;">// 3</span>  
 
  
 
[[Category:Operators]]
 
[[Category:Operators]]

Revision as of 08:51, 14 September 2011

Array : The array object

 

++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