Increments 
Author Message
 Increments

I am having difficulty understanding the following

i++
++i
What is the differences, I have a book on awk but somehow it does not
explain it correctly, I understand using
count = count + 1     which is equilevant to       count += 1

Please can someone explain the ++i  or i++,   ie: {variable++ or ++variable}
Thanks
Roms



Fri, 18 Jan 2002 03:00:00 GMT  
 Increments

Quote:

>I am having difficulty understanding the following

>i++
>++i
>What is the differences, I have a book on awk but somehow it does not
>explain it correctly, I understand using
>count = count + 1     which is equilevant to       count += 1

Try this:

BEGIN { i = 0; print ++i ; print i }

and then try:

BEGIN { i = 0; print i++ ; print i }

See the difference?



Fri, 18 Jan 2002 03:00:00 GMT  
 Increments

Quote:

>I am having difficulty understanding the following

>i++
>++i
>What is the differences, I have a book on awk but somehow it does not
>explain it correctly, I understand using
>count = count + 1     which is equilevant to       count += 1

>Please can someone explain the ++i  or i++,   ie: {variable++ or ++variable}

i++ post increments, i.e. increments it after using it

echo "a" |awk '{i=3;print $0, i++}'

will output

a 3

++i pre increments, i.e. increments it before using it

echo "a" |awk '{i=3;print $0, ++i}'

outputs

a 4

The code above is untested, but should work as explained.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 18 Jan 2002 03:00:00 GMT  
 Increments

Quote:



>>I am having difficulty understanding the following

>>i++
>>++i
>>What is the differences, I have a book on awk but somehow it does not
>>explain it correctly, I understand using
>>count = count + 1     which is equilevant to       count += 1

>>Please can someone explain the ++i  or i++,   ie: {variable++ or ++variable}

>i++ post increments, i.e. increments it after using it

>echo "a" |awk '{i=3;print $0, i++}'

>will output

>a 3

>++i pre increments, i.e. increments it before using it

>echo "a" |awk '{i=3;print $0, ++i}'

>outputs

>a 4

>The code above is untested, but should work as explained.

Better examples are:

echo "a" |awk '{i=3;print $0, i++; print "i =",i}'

output should be:

a 3
i = 4

echo "a" |awk '{i=3;print $0, ++i; print "i =",i}'

output should be:

a 4
i = 4

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 18 Jan 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Spin Box increments less than 1- from .0153 to .98438 w/.0153 increment changes

2. DolphinVM dll ref count not incremented?

3. Pattern to increment an instance variable

4. Incrementing

5. Auto Increment

6. Help incrementing print fields

7. Increment field?

8. Q: Increment field?

9. Auto-increment on dBase-table

10. Auto increment record

11. Auto Increment Error

12. CW2003:Auto-increment 9,999,999,999?

 

 
Powered by phpBB® Forum Software