Solved

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

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

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 101

Expert Comment

by:mlmcc
ID: 10319426
3 good explanations.

mlmcc
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
Introduction to Processes

724 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