What is the advantage of using the statement i++ instead of using i = i+1?

Questions by savithavelu   answers by savithavelu

Showing Answers 1 - 28 of 28 Answers

vamsisaikrishna, cbit

  • Jan 8th, 2007
 

Instead of i++ we could use i = i + 1. This would work, but may be less efficient, depending on the machine and the compiler used. Constructs like this were included in the original design of the C language so as to give hints to compiler, so that the latter could produce more efficient machine code. Of course, it also makes typing easier for the programmer.

  Was this answer useful?  Yes

praveen

  • Jan 15th, 2007
 

i++ response faster than the i=i+1;

so generally we use in our programming..

  Was this answer useful?  Yes

praveen

  • Jan 24th, 2007
 

i++ is the short form i=i+1, we reduce width of the line, it first get the variable & then increment it

  Was this answer useful?  Yes

sathish

  • May 14th, 2007
 

The actual reason for using i++ instead of using i=i+1 is i++ uses only one Assembly Instruction i.e. it uses INR instruction to increment the value. 

Thanks & Regards
Sathish Kumar

  Was this answer useful?  Yes

Less typing. 

There's no guarantee that the statement "i++;" would execute any faster than "i = i + 1;" -- a reasonably smart compiler such as gcc will recognize that the two statements are equivalent and generate identical machine code for both. 

Don't ever assume that one particular form of expression is faster than another; code up both versions and measure their performance directly, or compare the generated machine code.  "Tricky" code is not always faster than the more straightforward equivalent, and in some cases may be significantly slower. 

  Was this answer useful?  Yes

_jawahir_

  • Jun 17th, 2011
 

This is a shorthand method used commonly in programming languages for reducing codes and efforts.....there is more shorthand methods are there... some of them are..

(i=i+1)   =  (i++)
(i=i-1)  =  (i-=1)
(i=i*1)  = (i*=1)
(i=i/1)  = (i/=1)......... 

u can use any statements in this way......... try it.........  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions