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
Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to update exe applicatio from internet ? 6 86
printf performancy 11 68
C qsort compare function issue 6 16
Programming Language for Wordpress 7 33
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…

840 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