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

From Catglobe Wiki
Jump to: navigation, search
Line 1: Line 1:
<p style="color:#000099; font-size:14px;"><strong>Pre and Post Increment and Decrement</strong></p>
+
'''Pre and Post Increment and Decrement'''
<p>
+
 
&nbsp;</p>
+
&nbsp;
<p>
+
 
<span style="color:#000000;">++x is pre-increment&nbsp; and --x is pre-decrement</span></p>
+
<span style="color:#000000;">++x is pre-increment&nbsp; and --x is pre-decrement</span>
<p>
+
 
<span style="color:#000000;">x++ is post-increment and x-- is post-decrement</span></p>
+
<span style="color:#000000;">x++ is post-increment and x-- is post-decrement</span>
<p>
+
 
&nbsp;</p>
+
&nbsp;
<p>
+
 
<span style="color:#000000;">With ++x and --x: &nbsp;means x is incremented <strong>BEFORE</strong> being used.</span></p>
+
<span style="color:#000000;">With ++x and --x: &nbsp;means x is incremented '''BEFORE''' being used.</span>
<p>
+
 
<span style="color:#000000;">With x++ and x--: &nbsp;means x is incremented <strong>AFTER</strong> being used.</span></p>
+
<span style="color:#000000;">With x++ and x--: &nbsp;means x is incremented '''AFTER''' being used.</span>
<p>
+
 
&nbsp;</p>
+
&nbsp;
<p>
+
 
<span style="color:#a52a2a;"><span style="font-size: 12px;"><strong>Examples</strong></span></span></p>
+
<span style="color:#a52a2a;"><span style="font-size: 12px;">'''Examples'''</span></span>
<p>
+
 
&nbsp;</p>
+
&nbsp;
<p>
+
 
<span style="color:#000000;">number a = 1;</span></p>
+
<span style="color:#000000;">number a = 1;</span>
<p>
+
 
<span style="color:#000000;">number b;</span></p>
+
<span style="color:#000000;">number b;</span>
<p>
+
 
<span style="color:#000000;">b = ++a;</span></p>
+
<span style="color:#000000;">b = ++a;</span>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
<span style="color:#000000;">b = --a;</span></p>
+
<span style="color:#000000;">b = --a;</span>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
&nbsp;</p>
+
&nbsp;
<p>
+
 
<span style="color:#000000;">number c = 3;</span></p>
+
<span style="color:#000000;">number c = 3;</span>
<p>
+
 
<span style="color:#000000;">number d;</span></p>
+
<span style="color:#000000;">number d;</span>
<p>
+
 
<span style="color:#000000;">d = c++;</span></p>
+
<span style="color:#000000;">d = c++;</span>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
<span style="color:#000000;">d = c--;</span></p>
+
<span style="color:#000000;">d = c--;</span>
<p>
+
 
<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></p>
+
<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>
<p>
+
 
<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></p>
+
<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:Data_Types_Literals_and_Variables]]

Revision as of 05:01, 14 September 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