[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

postfix or prefix increment?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 40
Thread images: 2

File: for.png (4KB, 269x143px) Image search: [Google]
for.png
4KB, 269x143px
postfix or prefix increment?
>>
For a for loop that's absolutely irrelevant so you should do what all others are doing and use i++.
>>
>>61656778

In this case it produces the same results so pick the one to your linking.
>>
>Increase i by one
vs
>Increase one by i
I wonder which one is better
>>
i+=1
>>
>>61656832
I know, I'm asking whether it's considered better style to use one over the other. It seems to me that it would be better to use ++i since you don't need the original value returned by i++ (even if that's optimized away)
>>
>>61656778
++i makes more sense for a loop but that does not really matter. check the generate asm code to see how gcc handle it.
>>
File: wtfhow.jpg (21KB, 362x362px) Image search: [Google]
wtfhow.jpg
21KB, 362x362px
>>61656946
>It seems to me that it would be better to use ++i since you don't need the original value returned by i++ (even if that's optimized away)
You do realize ++i returns the original value +1... right?
>>
>>61656958
>++i makes more sense for a loop
Why do you say that?
>>
>>61657035
++i returns i after it's been incremented
i++ has to store the original value of i in a temporary register before it's incremented, then return that original value
>>
>>61656946
In both cases a value is returned (if you can be bothered to use it or it indeed gets optimized out).
>>
>>61657060
Yeah, but it won't because you're not using that value.
>>
>>61657072
i++ makes a copy of the original value. ++i doesn't. Using i++ implies that you need that original value.
>>
>>61656845
>>61657053
.....there's no reason it would be 'better'

this dude's talking out of his ass
>>
>>61657091
you need to initialize in both cases, what're you on about?
>>
>>61657113
int preincrement(int& i) {
i += 1;
return i;
}

int postincrement(int& i) {
int tmp = i;
i += 1;
return tmp;
}


Ideally they would be both the same in the for loop situation since any compiler worth using would optimize the copying away, but since ++i and i++ are both equally readable, why not use ++i?
>>
>>61657192
Cause operators are specified after operands?
>>
I find postfix style more readable. It literally doesn't matter as long as you're not using the result of the expression though.
>>
>>61657217
All other unary operators come before the operand in C: & * - + ~ ! sizeof
>>
>>61657053
++i increments the value then returns it.
i++ returns the value and then increments it.

++i reflects the intended behavior of a counter, nothing more.

>>61657096
Can't you fucking read?
I said
>++i makes more sense for a loop
which is true.
i++ or ++i will most likely be optimized in the same way by the compiler but writting exactly what you mean is the different between the knowledgable developer and the code monkey.
>>
incr i
>>
>>61657217
Operators come between their operands in infix notation, and unary operators typically come before their operand (unless you usually write 1- for "negative one").
>>
>>61657286
It seems that prefix is better in all circumstances.
I dont want to
int v = 13;
printf("%i", v++); //prints 13
//now v is 14

why use postfix?
>>
>>61657616
The typical case I can think of is combining something like:
a[i] = b;
++i;

into
a[i++] = b;
>>
>>61656808
>irrelevant
>so you should do what all others are doing and use i++.
???
>>
>>61657732
Something as important as a counter increase should have be on a dedicated line.
>>
>>61656868
/thread
>>
>>61657818
No.
>>
>>61657818
this
>>
>>61657616
Suffixed while as follows has values [0,c_initial-1]
while (c--) { }
This is obviously useful with arrays. On the other hand, similarly prefixed while would have values [1,c_initial-1], which is seldom useful.

>>61657818
This is just waste of space if the operation is otherwise an oneliner.
>>
>>61657818
I won't necessarily disagree, but >>61657732 is an extremely common idiom in C, so you're likely in the minority if that is your language of choice. Several other operators or the behaviour of certain operators in C were designed around shorthand like that.

And in that case, an increment operator is barely necessary at all. Languages that tend to take that stance would simply reuse
i = i + 1
.
>>
>>61658003
>is an extremely common idiom in C, so you're likely in the minority if that is your language of choice
That being said, garbage code like this and other shit like magic globals, reinventing linked lists instead of using proper dynamic array libraries with capacities, shit names and other crap are extremely common idioms in C. Since C programmers are mostly wannabe leet haxxorz and quality C coders are a rarity, it is safer to avoid most C code out there and people that produce stuff like
a[i++] = b;
, because those people would in the same breath produce stuff like
a = a++;
.
>>
>>61656832
No it doesn't, the prefix version skips the 0'th index. Which might be want you want, or it might not be want you want. Depends on what you intend for the loop to do.
>>
>>61658183
inb4
>I was merely pretending to be retarded
>>
.map() or . flatMap() and no incrementing because i'm not a c/c++ pajeet
>>
>>61658140
Aside from that, the
a[i++] = b;
usage is in K&R C, so it's not reserved solely for "garbage" coders. Like basically all of C in general it's a shorthand that you can use or abuse.
>>
>>61658777
K and R are garbage coders for not learning from the past, creating C and allowing its standard in such a limited way in the first place.
What a waste of trips.
>>
>>61656868
i = i + 1
>>
for (i = 0; i < MAX; asm volatile("inc %0" :  : "=r"(i)))
>>
>>61658325
This
Thread posts: 40
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.