Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Jun 30, 2000

Avoid Increment/Decrement Operators in Expressions

You should avoid usage of increment and decrement operators inside other expressions. Increment operator (++) adds 1 to the current value of the variable and decrement operator (--) subtracts 1 from it. There are two forms in which these operators can be used postfix and prefix.

The difference between these two forms appears when they are used inside expressions. The prefix form does the addition (or subtraction) first; the postfix form evaluates to the old value of the variable before incrementing (or decrementing) it.

I think usage of these operators inside other expressions results in less readable code which at times also causes bugs that are real hard to nail down.

Consider following scenario:
 
int i = 10, j = 10;
int m = (2 * ++i);	// m becomes 22 and i is 11
int n = (2 * j++);	// n becomes 20 and j is 11
Deepak Pant
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date