pre-increment

Post-increment within a self-assignment

心不动则不痛 提交于 2019-11-26 19:49:10
问题 I understand the differences between i++ and ++i, but I'm not quite sure why I'm getting the results below: static void Main(string[] args) { int c = 42; c = c++; Console.WriteLine(c); //Output: 42 } In the above code, as this is assigning the variable to itself and then incrementing the value, I would expect the result to be 43 . However, it is returning 42 . I get the same result when using c = c--; as well. I realise I could just simply use c++; and be done with it, but I'm more curious

++i or i++ in for loops ?? [duplicate]

隐身守侯 提交于 2019-11-26 18:52:37
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is there a performance difference between i++ and ++i in C++? Is there a reason some programmers write ++i in a normal for loop instead of writing i++ ? 回答1: For integers, there is no difference between pre- and post-increment. If i is an object of a non-trivial class, then ++i is generally preferred, because the object is modified and then evaluated, whereas i++ modifies after evaluation, so requires a copy to

Incrementor logic

你离开我真会死。 提交于 2019-11-26 18:52:22
I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression : public static void main(String[] args) { int i = 0; i = i+=(++i + (i+=2 + --i) - ++i); // i = 0 + (++i + (i+=2 + --i) - ++i); // i = 0 + (1 + (3 + 2) - 1); // i = 0 + (6 - 1); System.out.println(i); // Prints 0 instead of 5 } I know I'm missing the logic somewhere but where? What I've tried : Going from left to right (though I know it is not recommended) Going from the insidest bracket and starting from there. Thanks for the help PS : The comments are the details of my calculus EDIT 1 I

Expressions "j = ++(i | i); and j = ++(i & i); should be a lvalue error?

落花浮王杯 提交于 2019-11-26 18:11:49
问题 I was expecting that in my following code: #include<stdio.h> int main(){ int i = 10; int j = 10; j = ++(i | i); printf("%d %d\n", j, i); j = ++(i & i); printf("%d %d\n", j, i); return 1; } expressions j = ++(i | i); and j = ++(i & i); will produce lvalue errors as below: x.c: In function ‘main’: x.c:6: error: lvalue required as increment operand x.c:9: error: lvalue required as increment operand But I surprised that above code compiled successfully, as below: ~$ gcc x.c -Wall ~$ ./a.out 11 11

Order of operations for pre-increment and post-increment in a function argument? [duplicate]

三世轮回 提交于 2019-11-26 17:48:30
This question already has an answer here: Why are these constructs using pre and post-increment undefined behavior? 14 answers I have some C code: main() { int a=1; void xyz(int,int); xyz(++a,a++); //which Unary Operator is executed first, ++a or a++? printf("%d",a); } void xyz(int x,int y) { printf("\n%d %d",x,y); } The function xyz has two parameters passed in, ++a and a++ . Can someone explain the sequence of operations to explain the result? The above code prints "3 13" or "2 23" depending on which compiler is used. Well, there are two things to consider with your example code: The order

Is the pre-increment operator thread-safe?

假装没事ソ 提交于 2019-11-26 16:59:10
问题 I'm making a program in java that races a few cars against each other. Each car is a separate thread. When cars complete the race, each one all calls this method. I've tested the method at varying timer speeds, and it seems to work fine. But I do realize that each thread is accessing the variable carsComplete, sometimes at the exact same time (at least at the scope the date command is giving me). So my question is: is this method thread-safe? public static String completeRace() { Date

What is more efficient, i++ or ++i? [duplicate]

China☆狼群 提交于 2019-11-26 16:28:09
Exact Duplicate : Is there a performance difference between i++ and ++i in C++? Exact Duplicate : Difference between i++ and ++i in a loop? What is more efficient, i++ or ++i? I have only used this in Java and C/C++, but I am really asking for all languages that this is implemented in. In college I had a professor show us that ++i was more efficient, but it has been a couple of years, and I would like to get input from the Stack Overflow community. i++ : create a temporary copy of i increment i return the temporary copy ++i : increment i return i With optimizations on, it is quite possible

Java increment and assignment operator

只谈情不闲聊 提交于 2019-11-26 14:17:49
问题 I am confused about the post ++ and pre ++ operator , for example in the following code int x = 10; x = x++; sysout(x); will print 10 ? It prints 10,but I expected it should print 11 but when I do x = ++x; instead of x = x++; it will print eleven as I expected , so why does x = x++; doesn't change the the value of x ? 回答1: No, the printout of 10 is correct. The key to understanding the reason behind the result is the difference between pre-increment ++x and post-increment x++ compound

The difference between ++Var and Var++ [duplicate]

天涯浪子 提交于 2019-11-26 10:27:34
问题 This question already has an answer here: How do the post increment (i++) and pre increment (++i) operators work in Java? 14 answers In programming, particularly in Java, what is the difference between: int var = 0; var++; and int var = 0; ++var; What repercussions would this have on a for loop? e.g. for (int i = 0; i < 10; i++) {} for (int i = 0; i < 10; ++i) {} 回答1: tldr; Although both var++ and ++var increment the variable they are applied to, the result returned by var++ is the value of

Pre- & Post Increment in C#

ぐ巨炮叔叔 提交于 2019-11-26 07:37:50
问题 I am a little confused about how the C# compiler handles pre- and post increments and decrements. When I code the following: int x = 4; x = x++ + ++x; x will have the value 10 afterwards. I think this is because the pre-increment sets x to 5 , which makes it 5+5 which evaluates to 10 . Then the post-increment will update x to 6 , but this value will not be used because then 10 will be assigned to x . But when I code: int x = 4; x = x-- - --x; then x will be 2 afterwards. Can anyone explain