Solved

How is the increment operator significant to C, C++, and Java?

Posted on 2003-11-19
7
564 Views
Last Modified: 2008-03-17
How is the increment operator significant to C, C++, and Java?
Could you explain to me how it works and why it is important to have increment operator?
0
Comment
Question by:veerawudth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 43 total points
ID: 9784557
I'll give you a little of the history.

If you are familiar with why C was developed and the original platform then the increment and decrement operators make sense.

The original hardware platform had a hardware increment and decrement capability which was much faster than x = x + 1;

C was developed to write an operating system (specifically Unix)  Any chance at speed increases were taken.  Thus the birth of the ++ and - - operators.

With the speed of machines today I am not sure it is even implemented the same way.  C++ has it to maintain compatability with C.  Java grew out of C and C++ so it was included.

mlmcc
0
 
LVL 22

Assisted Solution

by:cookre
cookre earned 41 total points
ID: 9784641
That's one of the characteristics that led many folks to say that C was really just a long winded assembly language.  Many CPUs had a feature that minimized looping times by using bits in an instruction that activate automatic index register incrementation or decrementation either before or after the effective address for that instruction is calculated - without effecting instruction execution time.  Hence, for example, an assembly program's use of index register pre-incrementation would be echoed in a c program with ++i, post incrementation with i++, and similarly with --.

The construct i+=1 (as opposed to i=i+1), as mlmcc indicated, was added to allow compiler writers to make use of increment (and decrement) instructions, e.g.:
   inc i                
at the cost of just a single cycle, as opposed to
   la       a0,i
   aa,u   a0,1
   sa      a0,i

costing three cycles.
0
 
LVL 18

Assisted Solution

by:Cluskitt
Cluskitt earned 41 total points
ID: 9786530
The incremental operator is basically very handy for programming, regardless of its speed contribute. With the incremental factor you can, AT THE SAME TIME, make an output and an attribution. For example, printf("%i",i++); will print on the screen the value of i, and it will increment it. So, if i was 10 before that line, it will print 10 on the screen and i will have a new value of 11. Similarly, if you type, printf("%i",++i), and again assuming i was 10, it will first increment it, and then print the output to the screen, in this case, 11.
There are plenty of uses where the incremental operator is handy, and some where it's a wonderful thing. Also, there are some where it's just a way to make it different, or writing a bit less.
And it avoids the thing that most confuses newcomers to programming which is, i=i+1 (usual comments being, i=i+1<=>0=1, which makes no sense ;-)
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 10319426
3 good explanations.

mlmcc
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question