Solved

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

Posted on 2003-11-19
7
561 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now