Do you prefer prefix or postfix increment when you aren't using the returned value?

Do you prefer prefix or postfix increment when you aren't using the returned value?

Other urls found in this thread:

youtu.be/lrtcfgbUXm4
physics.rutgers.edu/~wksiu/C /MoreEC _only.pdf#page=32
open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf#page=120
idinews.com/peeves/prefixIncr.html
twitter.com/SFWRedditImages

postfix

i++

Postfix.

+i+

post

fonction increment(x) {
if(x==0){
x = 1;
} elseif(x==1) {
x = 2;
} elseif(x==2) {
x = 3;
} elseif(x==3) {
x = 4;
} elseif(x==4) {
x = 5;
} elseif(x==5) {
x = 6;
} elseif(x==6) {
x = 7;
} else {
x = "stack overflow";
}
return x;
}

i = i + 1
These cryptic symbol are just confusing lol. Why do guys always complicate things???

++i solely because I feel it makes me look better for some reason

A true patrician's choice, my fellow Lua using gentleman!

Go away, Guido.

int i = 5;
i += ++i+i++


what's the value of i now Sup Forums?

if i dont need the value, it's fucking ++i. i++ by definition wastes more memory

??

21

16?

This

I dont write spaghetti code

LET i = i +1

There's a difference between the two so I use them according to the situation
i++ when I need the change in the next iteration
++i when I need the change to be immediate

pathetic

I prefer postfix as long as the compiler is going to optimize away the performance hit that would otherwise come with using postfix

doing ++i or i++ isn't spaghetti code you retard

Does anyone here actually know C

13? But im not sure actually

>i++ when I need the change in the next iteration
>++i when I need the change to be immediate
u wot

24?

looks like Javascript does both ++i and i++ before the rest.

But I think that's wrong in other languages.

++i because I didn't know there were other options.
>everything I know I learned on Sup Forums.

He means when you use a for loop.

for(i=0;i 0,1,2
for(i=0;i 1,2,3

I think that was the whole point of having both versions.

So C prints 20.

Here's what the assembler is doing:

movl $5, 28(%esp)
addl $1, 28(%esp)
movl 28(%esp), %eax
leal 1(%eax), %edx
movl %edx, 28(%esp)
movl 28(%esp), %edx
addl %edx, %eax
addl %eax, 28(%esp)
movl 28(%esp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)

java prints 17

LMAO'ing @ THIS THREAD
youtu.be/lrtcfgbUXm4
An Indian (who Sup Forums is likely to insist is an idiot) is able to demonstrate the functional difference.

The i++/++i isn't executed until the end of the loop. There's no difference in output between those two.

y=2+2
x=y-1=3
elseif(x=3)
x=quick mafs

>++i, retuns 6, i=6
>i++, returns 6, i=7
>i += 6+6
12+7=19
but this smells of undefined behavior.

L0
LINENUMBER 165 L0
ICONST_5
ISTORE 1
L1
LINENUMBER 166 L1
ILOAD 1
IINC 1 1
ILOAD 1
ILOAD 1
IADD
IADD
ISTORE 1

This is undefined behavior, but let's parse it like a gcc would

i starts off as 5, and we have the clarified code:
i += (++i)+(i++)
The left-hand side of the + operator will be evaluated first. It is a preincrement, so i is now 6 and we replace that token with 6, leaving us with
i += 6+(i++)
Now we execute the right-hand side, which is postincrement, so we replace the token with the current value (6) and then set i to 7. So now we have
i += 6+7 where i is currently 7.
This leaves us with an i of 20

If you compile this with clang, you would get 19 instead of 20 due to differences in the implementation of undefined behavior

19

Undefined behavior.

1) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.

++i executes the command i = i+1 immediately
i++ executes the command i = i+1 after the iteration in a loop

That means that you can write i++ and throughout and use the old i value because it will only change after the iteration is done

Open any IDE, put a watch on both ++i and i++ and see the difference

>Do you prefer prefix or postfix increment when you aren't using the returned value?
>when you aren't using the returned value?

5+6+5 = 16

He is using the returned value, retard. It has literally nothing to do with this thread at all. How fucking embarrassing it must be to be so clueless that you would even write this reply.

Pretty sure c does prefix/postfix before addition, so 18 maybe? Or it fucks up and it's 7

Oh just saw . I guess when you modify i in the right hand side of a +=, it uses the value after modification. Makes sense in the context of compilation.

How retarded would your compiler need to be for this to be the case

>when you aren't using the returned value
You're supposed to use prefixed for this.

here's a better explanation,

i=1;
j=++i ;
>In this case, if your print j, j will be 2

i=1;
j=i++;
>in this case, j will be the old i value, j=1
>here, the incrementation is not the main operation but a side effect

Think about how it's implemented. ++i increments i and returns the new value.

i++ stores the current value separately, then increments i and returns the stored value. This uses more memory for the extra variable.

for (int i = 0; i < n; ++i)
is equivalent to
int i = 0;
while (i < n) {
++i;
}

for (int i = 0; i < n; i++)
is equivalent to
int i = 0;
while (i < n) {
i++;
}

There is no difference between the two.

That's not how it works in C++ or Java or any other normal language. Try opening up an IDE yourself and checking it.

And using prefix increment in loops may have an advantage in C++ because it eliminates an intermediate value that the compiler may not be able to eliminate. Scott Meyers goes into detail in one of his books.

You guys are retarded. Using preincrement or postincrement in the last term of a for loop doesn't change anything at all. The last loop term isn't executed until the end of the loop body, and the return value (aka the only difference between pre and post-increment) is discarded

+;
i;
+;

I hope you dickheads are using C because it’s clear that you never read Effective C++.

refer hereIt only works when you give the value of i++/++i to something, not when you use it as a standalone command

i++
>incrementation is a side effect that happens after the value of i is handed somewhere
++i
>incrementation is the operation and has no side effect, the incremented value is given

The simplest way is to just read it, I don't know how this is difficult to understand.
>++i
>increment, then return i
>i++
>return i, then increment

wrong
right

>It only works when you give the value of i++/++i to something
Read the fucking OP

Also the return value of the statement in the third for loop term is discarded.

>retarded compiler
If you’re just a C wonk: yeah, sure.

If you ever use C++:

Stupid fuck: the autoincrement and autodecrement operators can be overloaded.

Step aware from the fucking keyboard and read Scott Meyers’ Effective C++.

Goddamnit, fucking idiots think since they type: “make” they’re Adrei Alexandrescu...wait...check that they don’t know who that is, maybe Herb Sutter...nope him neither...they probably think they’re Stroustrup.

Why don't you explain how I'm wrong, dickhead? Here's why I'm right:

physics.rutgers.edu/~wksiu/C /MoreEC _only.pdf#page=32

open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf#page=120

>hurr fucking durr

for (int i = 0; i < 4; i++) {
printf("%d ", i);
}
0 1 2 3

for (int i = 0; i < 4; ++i) {
printf("%d ", i);
}
0 1 2 3

Wow look, it's the same in C

> ++i
increment, then return the new value
> i++
store the old value in a register, increment, then return the old value

Gee, I wonder which I should use?

>having smart compiler
bloat.

OP specifically said when the return value isn't used, in which case any compiler will compile them to the exact same instructions

Learn to read before you try to spout common knowledge that doesn't apply to the situation

There's a chance a shitty compiler would still do the copy even when it isn't necessary. It may be a 0.0001% chance of happening, but why choose 0.0001% over 0%?

>when you aren't using the returned value
gee, I wonder if you're retarded

Isn’t it horrifying how many C++ people haven’t read Scott Meyers’ books?

Especially since every one of them has strong opinions.

Strong *wrong* opinions.

>shitty compiler
are you mentally ill?

There is no compiler in common use that would do that

Programming as if you were using an actively bad compiler is not a good way to go through life

...

What a retarded paragraph

++i
idinews.com/peeves/prefixIncr.html

I only program in Java.

i -= -1

Good.

I mostly use C but I carry all my styles over when I have to write something in C++

Scott Meyers is a hack

You’re either a great troll or a shite coder.

I'm an EE. Upsetting code monkeys is one of the many joys of my job

THIS

I’m sure you find a way to suck at VHDL too.

kek

Neither is relying on compiler optimizations to be there when you can just use something that's always going to be correct.

...

#define i++ (2+i--)

that's not an lvalue

I once found code that did
j = a[i++] + a[i++]

A nasal demon.

++i = cum guzzling faggots
++i = Aryan Alpha Males

good job fag

I just do i += 1 because I have a job and need to move on to more important things like fixing the css

I'm a total postfix guy

no it can just reuse the same register, i.e. load i into rax, execute your statement, add one to rax, save rax back into memory.

++i versus i++ just swaps the order of your statement and the addition.

Also, i will probably already be in a register anyways so it won't even matter

Prefix because it doesn't use a temporary variable. I know the compiler optimizes it away but I'm autistic like that.

++i is faster.

Undefined

always use postfix except you explicitly want the value to increase after the assignment ... but don't do that if possible.

meant prefix (++i), sorry

Where's the semicolon, syntax smygmoid?

C++ is such a nasty piece of shit you need to read a book of "items" to not fuck up in one of C++'s many man traps. I mean I was an acolyte for decades but spend a year in Java and you'll never want to write unique_ptr again.

The answer is obviously 17.

You can re-write this as follows

5+6+5

After that returns i is incremented by 1 beause of the postfix operator.

the answer is -1.

rewrite as (-1) += (-1++)+(-1++). incrementing -1 = 0.

rewrite as (-1) += 0 + 0 = -1

this thread is now all about templeos

Postfix. Just as a convention. I don't use prefix unless I have a specific need for it, because prefix is more of a pain in the ass when I'm debugging with DDD/GDB.

>gcc
>>warning: operation on ‘i’ may be undefined [-Wsequence-point]
prints 20

>clang
>>warning: unsequenced modification and access to 'i' [-Wunsequenced]
prints 21

So it's undefined behaviour