Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
In the original C language, and many languages that have C-like syntax, there are (at least) three ways to increment a variable (by 1). Assume the variable is an integer called count1. Here are three ways.
count1++;
count1 += 1;
count1 = count1 + 1;
3. Code increments
One sometimes wants to look at the generated code to see how statements might be written to run faster (i.e, by taking fewer statements)
Here is the Java code.
Here is the output of the Java code.
4. Generated code
From the relevant generated code, we see the following. Here is the Java source code fragment.
count1++;
count1 += 1;
count1 = count1 + 1;
Here is the generated byte-code separated into groups for each source code statement.
The first two source code statements generate the same code. The last one generates more code - involving a load and a store. (Some compilers would improve the generated code).
5. Python example
Python does not have the "++" increment operator.
Here is the Python code.
Here is the output of the Python code.
The code appears to be the same size but has two different instructions.
6. Python example
Let us see how Python compiles an arithmetic expression.
Here is the Python code.
The code compiles to and is evaluated by a postfix stack machine.