English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

7 answers

In C there is a Pre and Post INcrement

++i is pre increment
i++ is post increment

Yes both are incremented but what is different is WHEN.
Preincrement the increment happens upon entering the line of code first. Post increment the increment happens when leaving the code

These two lines will give you different results : (Assume i=0 when considering the remarks:

myArray[i++] = get_value(); //returns myArray(0)
vs
myArray[++i] = get_value(); //returns myArray(1)


Increment methods can also be Relative vs Absolute a good example of relative incrementing is with pointers. You first reference or point to a memory location of a variable then increment from that point.

Absolute is when you would define a starting point like i=0 to define the first index position in an array rather than by using a pointer

2007-06-21 01:29:23 · answer #1 · answered by MarkG 7 · 0 0

While various 3GL languages use different syntax, they all generate the same machine language.

That is, the variable is put in a register, the register is incremented, and the register contents is stored at the variable's location.

Thus COBOL's
INCREMENT VAR1 BY ONE
does exactly the same thing as Fortran's
VAR1 = VAR1 + 1
and Pascal's
VAR1 := VAR1 + 1;

The C language has two different increment operators. The "+=" operator increments the variable before using it, and the "=+" operator increments the variable after using it .

That is:
a = b = 5.
c = 15 + (a++);
d = 15 + (++b);
After executing these statements, both a and b have been incremented to 6. However, c has the value 20, and d has the value 21.

2007-06-21 01:27:51 · answer #2 · answered by Anonymous · 0 0

It really depends on the programming language you use but for the most part to increment the value of a variable you simply set it equal to 1 plus itself.
ie. i = i + 1

When this statement is placed in a loop with conditions the value will continue to increment until the loop conditions are met.
ie.
i = 2
Do
Msgbox("Hello, this loop has run " & i & " times.")
i = i+1
Loop Until i = 5

In the above loop the message should print 4 times.
Hello, this loop has run 2 times
Hello, this loop has run 3 times
Hello, this loop has run 4 times
Hello, this loop has run 5 times

On the fourth loop the value of 'i' would equal 5 and the loop condition would be met. This sample is from VBscript. Other programming languages will differ slightly of course.

2007-06-21 01:11:03 · answer #3 · answered by gobz03 1 · 0 0

i=i+1;
i++;
i=+1;

2007-06-21 01:08:21 · answer #4 · answered by ihoston 3 · 1 0

two methods:

1.pre-increment : ++i which means
i=i+1;
i=i;
2.post-increment : i++ which means
i=i;
i=i+1;

2007-06-21 03:07:47 · answer #5 · answered by sathiyendran a 3 · 0 0

int i;
1- i=i+1
2- i+=1
3- i++
4- ++i
5- inc(i) this function varies depend on the language in use

2007-06-21 01:04:33 · answer #6 · answered by Just Me 2 · 2 0

The "i=i+1" method mentioned above works just peachy when put in a "FOR-NEXT" loop.

(I used to play with old line numbered BASIC... not too current on the "new fangled stuff.) ;-)

2007-06-21 01:07:59 · answer #7 · answered by Sam84 5 · 0 1

fedest.com, questions and answers