Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
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…

760 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

23 Experts available now in Live!

Get 1:1 Help Now