Thread: incrementing
View Single Post
  #4   Spotlight this post!  
Unread 07-12-2006, 23:19
TubaMorg TubaMorg is offline
Programmermechanicalelect ricalcoach
AKA: Dan
FRC #1480 (Robatos Locos)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Houston
Posts: 450
TubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond repute
Re: incrementing

i++ is shorthand for i = i + 1 since incrementing by one is such a common task in programming. Similarly i-- is the same as i = i -1. If you want to add or subtract something different then you have to use the long version as in i = i + 3.


Not many people realize you can also go ++i and --i which is subtly different. In most cases the end effect is the same but not always. With ++i (prefixed) the value is computed before the expression is evaluated. The postfix version i++ is the opposite where the value is computed AFTER the expression is evaluated. This doesn't make any difference if you just go:

i++;
or
++i;

However if you try:

m = ((++i)*(j++)/5;

i will increment by 1 BEFORE the rest of the expression is evaluated. j will be incremented AFTER the expression is evaluated.

Good luck!