Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You actually have it the wrong way around:</p> <p><code>i=i+1</code> is closer to <code>++i</code> than to <code>i++</code></p> <p>The reason for this is that it anyway only makes a difference with surrounding expressions, like:</p> <p><code>j=++i</code> gives j the same value as <code>j=(i+=1)</code> and <code>j=(i=i+1)</code></p> <hr> <p>There's a whole lot more to the story though:</p> <p>1) To be on the same ground, let's look at the difference of pre-increment and post-increment:</p> <p><code>j=i++</code> vs <code>j=++i</code></p> <p>The first one is <strong><em>post-increment</em></strong> (value is returned and "post"=afterwards incremented), the later one is <strong><em>pre-increment</em></strong> (value is "pre"=before incremented, and then returned)</p> <p>So, (multi statement) equivalents would be: <code>j=i; i=i+1;</code> and <code>i=i+1; j=i;</code> respectively</p> <p>This makes the whole thing very interesting with (theoretical) expressions like <code>j=++i++</code> (which are illegal in standard C though, as you may not change one variable in a single statement multiple times)</p> <p>It's also interesting with regards to memory fences, as modern processors can do so called out of order execution, which means, you might code in a specific order, and it might be executed in a totally different order (though, there are certain rules to this of course). Compilers may also reorder your code, so the 2 statements actually end up being the same at the end.</p> <p>-</p> <p>2) Depending on your compiler, the expressions will most likely be really the same after compilation/optimization etc.</p> <p>If you have a standalone expression of <code>i++;</code> and <code>++i;</code> the compiler will most likely transform it to the intermediate 'ADD' asm command. You can try it yourself with most compilers, e.g. with gcc it's <code>-s</code> to get the intermediate asm output.</p> <p>Also, for many years now, compilers tend to optimize the very common construct of</p> <p><code>for (int i = 0; i &lt; whatever; i++)</code></p> <p>as directly translating <code>i++</code> in this case would spoil an additional register (and possible lead to register spilling) and lead to unneeded instructions (we're talking about stuff here, which is so minor, that it really won't matter unless the loop runs trillion of times with a single asm instruction or the like)</p> <p>-</p> <p>3) Back to your original question, the whole thing is a precedence question, as the 2 statements can be expressed as (this time a bit more exact than the upper explanation):</p> <p><code>something = i++</code> => <code>temp = i; i = i + 1; something = temp</code></p> <p>and</p> <p><code>something = ++i</code> => <code>i = i + 1; something = i</code></p> <p>( here you can also see why the first variant would theoretically lead to more register spilled and more instructions )</p> <p>As you can see here, the first expression can not easily be altered in a way I think would satisfy your question, as it's not possible to express it using precedence symbols, e.g. parentheses, or, simpler to understand, a block). For the second one though, that's easy:</p> <p><code>++i</code> => <code>(++i)</code> => <code>{ i = i + 1; return i }</code> (pseudo code)</p> <p>for the first one that would be</p> <p><code>i++</code> => <code>{ return i; i = i + 1 }</code> (pseudo code again)</p> <p>As you can see, this won't work.</p> <hr> <p>Hope I helped you clear up your question, if anything may need clarification or I made an error, feel free to point it out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload