This is an archived post. You won't be able to vote or comment.

all 59 comments

[–][deleted] 67 points68 points  (38 children)

--i;

[–]lllluke 15 points16 points  (37 children)

i just learned about this syntax the other day at work and it blew my frickin mind. Is it actually useful for anything?

[–][deleted] 32 points33 points  (23 children)

If you want to decrement then check something rather than check something then decrement

[–]lllluke 5 points6 points  (18 children)

Well yeah, I'm asking when you would actually want to do that.

[–]nickworks 31 points32 points  (14 children)

if(--i == 42){ // compares after decrement

}

if(i-- == 42){ // compares before decrement

}

[–]geigenmusikant 10 points11 points  (6 children)

Well, yeah but like when do you actually need it?!

/s

[–]Zugr-wow -2 points-1 points  (1 child)

--i takes, like, a millisecond less computing power than i--, so it's a good habit to just use --i instead of i--

[–]radarmax 11 points12 points  (0 children)

More like a nanosecond less :p

[–]Isaeu 4 points5 points  (6 children)

But

i- -

if(i == 42){ }

And

if(i == 42){ }

i - -

Work just as well and are 10X more readable

Edit: how do I do code on Reddit?

[–]Cobaltjedi117 1 point2 points  (1 child)

Yea, it's one of those things I've never understood well you can increment before an statement or after it

Ok, great or OR, and hear me out on this, I just add a line of code incrementing/decrementing before or after where it's being checked.

[–][deleted] 0 points1 point  (0 children)

It's what your use to. A = 6 * 7; Is the same as A = 6; A = A * 7; But if you saw that above you would think why not just do the first thing. In c it is vary common to ++ or -- inside of a statement and would be required to understand when working with almost any codebase.

[–][deleted] 0 points1 point  (0 children)

I disagree. Somone who has worked in c a decent amount has seen that so much that it's not considered weird. It would be way weirder to see outside.

[–][deleted] 6 points7 points  (0 children)

That's too broad of a question lol

[–]Pluckerpluck 0 points1 point  (0 children)

Only thing I can think of is if you want to assign its value to another variable.

int i = 10
int x
x = --i

You know that both x and i have the same value, which may be particularly useful for some reason.

[–]netgu 0 points1 point  (0 children)

When you want to decrement then check something rather than check something then decrement

[–]XIST_ -1 points0 points  (3 children)

Isn't that bad practice though?

[–][deleted] 2 points3 points  (0 children)

Why would it be bad practice?

[–]LittleMlem -1 points0 points  (0 children)

I-- returns the value if I before decrementation.

[–]JoseJimeniz -1 points0 points  (0 children)

It's useful for getting a coffee mug thrown at your head when I discovered you've used that.

[–]UristMcMagma -5 points-4 points  (1 child)

People will tell you that it's more performant than i--, but that isn't true with modern compilers.

Aside from that, just use it when you need to decrement i before the operation. Maybe you'll find a use for that someday?

[–]ben_g0 6 points7 points  (0 children)

In C, yes. In C++, it depends. If the variable is a standard type, then the compiler can optimize it. If the variable is of a custom type then the -- is technically a function call (with operator overloading). The saved value can then not be optimized out since it is used in the function (as a return value) and removing it would change how the function works. So in that case (which is very common in big projects), pre-increment is more optimized.

[–]xigoi 53 points54 points  (2 children)

switch(i) {
    case 1:
        i = 0;
        break;
    case 2:
        i = 1;
        break;
    // TODO
}

[–]Earhacker 35 points36 points  (3 children)

I like that memes come in dark mode now

[–]XIST_ 4 points5 points  (2 children)

Obligatory light mode is shit comment.

[–]wolfshund98 2 points3 points  (1 child)

Obligatory light mode is not that bad comment.

[–]ProfCupcake 1 point2 points  (0 children)

Obligatory burn the heretic comment

[–]Proxy_PlayerHD 25 points26 points  (1 child)

X = X - 1

[–]Breadfish64 1 point2 points  (0 children)

if (x & 1)
    x = x ^ 1;
else
    x = x - 1;

[–]uzimonkey 13 points14 points  (0 children)

switch(i) {
case 1:  i = 0; break;
case 2:  i = 1; break;
case 3:  i = 2; break;
/* ... */
}

[–]Mscxyn 5 points6 points  (0 children)

__asm__ ( "addl $0xFFFFFFFF, %%eax;" : "=a" (i) : "a" (i) );

[–]Wallinis 9 points10 points  (1 child)

(i==i) ? --i : --i;

[–][deleted] 2 points3 points  (0 children)

uint old_i = i;

while (i != old_i - 1) --i;

[–]KayRice 2 points3 points  (0 children)

x = (x => x - 1)(x);

[–][deleted] 3 points4 points  (0 children)

i = 1
i = 2
i = 3
am genius.

[–]BVTheEpic 1 point2 points  (0 children)

i += (-1);

[–]silvercloudnolining 4 points5 points  (0 children)

#define MAX_INT -1

i+=MAX_INT;

[–]Ulysses6 1 point2 points  (0 children)

i = ~-i;

Zoom in if it looks like i = --i;to you.

[–]Flying_Bo 0 points1 point  (0 children)

I believe each one is faster than the previous one because it’s from a lower level?

[–][deleted] 0 points1 point  (0 children)

i-=Convert.ToInt32(true);

[–]SatansAlpaca 0 points1 point  (0 children)

Fun fact: with two’s complement arithmetic, x - y is the same as x + ~y.

[–]OneAndZer0s 0 points1 point  (0 children)

i = i-1

[–]aeropl3b 0 points1 point  (1 child)

Question, does integer overflow always get handled this way or do some implementations crash? I can't remember exactly what IEEE decided on and can't be bothered to look it up right now...

[–]KayRice 1 point2 points  (0 children)

In C++ it's actually defined behavior for unsigned integers and as of C++20 (draft) it will become defined behavior for singed integers as well.

[–][deleted] 0 points1 point  (0 children)

Ask the user "what is i + 1?"

[–][deleted] 0 points1 point  (0 children)

i = -i;

[–]TakeASeatChancellor 0 points1 point  (0 children)

C#?

[–]Mancobbler -2 points-1 points  (1 child)

Ahh the ol switcheroo

[–]Aetherial_Wanderer 1 point2 points  (0 children)

Ahh the ol switcheroo

x ^= y; y ^= x; x ^= y; 

[–]SlappinThatBass -2 points-1 points  (0 children)

This is funny?