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

From Catglobe Wiki
Jump to: navigation, search
Line 17: Line 17:
 
<span style="color:#a52a2a;"><span style="font-size: 12px;">'''Examples'''</span></span>  
 
<span style="color:#a52a2a;"><span style="font-size: 12px;">'''Examples'''</span></span>  
  
 +
<br>
  
 
+
<span style="color:#000000;" />
<span style="color:#000000;">number a = 1;</span>
+
<source lang="">number a = 1;
 
+
number b;
<span style="color:#000000;">number b;</span>
+
b = ++a;
 
+
print(b);               // 2
<span style="color:#000000;">b = ++a;</span>
+
print(a);               // 2
 
+
b = --a;
<span style="color:#000000;">print(b);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#008000;">// 2</span>
+
print(b);               // 1
 
+
print(a);               // 1</source>
<span style="color:#000000;">print(a);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#008000;">// 2</span>
+
<span style="color:#008000;" />  
 
 
<span style="color:#000000;">b = --a;</span>
 
 
 
<span style="color:#000000;">print(b);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#008000;"> // 1</span>
 
 
 
<span style="color:#000000;">print(a);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#008000;">// 1</span>  
 
  
 
&nbsp;  
 
&nbsp;  

Revision as of 08:46, 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