post-increment

What is x after “x = x++”?

浪尽此生 提交于 2019-11-28 01:47:09
What happens (behind the curtains) when this is executed? int x = 7; x = x++; That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement . In my book, it says that x is incremented! Mysticial x does get incremented. But you are assigning the old value of x back into itself. x = x++; x++ increments x and returns its old value. x = assigns the old value back to itself. So in the end, x gets assigned back to its initial value. Prince John Wesley x = x++; is equivalent to int tmp = x; x++; x = tmp

When to use post increment and pre increment in Java [duplicate]

孤者浪人 提交于 2019-11-28 01:26:28
问题 This question already has answers here : How do the post increment (i++) and pre increment (++i) operators work in Java? (14 answers) Closed 3 years ago . I understand that there are a number of questions on this topic on StackOverflow. But I am still slightly confused and unsure of when to use the operations. I am going through old tests in studying for my exam. One of the methods returns the number of classrooms that are handicapped accessible and are available. I wrote the counter method

Java: pre-,postfix operator precedences

為{幸葍}努か 提交于 2019-11-27 23:40:30
I have two similar questions about operator precedences in Java. First one: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 According to Oracle tutorial : postfix (expr++, expr--) operators have higher precedence than prefix (++expr, --expr) So, I suppose that evaluation order: 1) first postfix operator: X++ 1.a) X++ "replaced" by 10 1.b) X incremented by one: 10+1=11 At this step it should look like: System.out.println(10 * ++X * X++), X = 11; 2) second POSTfix operator: X++ 2.a) X++ "replaced" by 11 2.b) X incremented by one: 11+1=12 At this step it should look like: System

Post-increment within a self-assignment

瘦欲@ 提交于 2019-11-27 19:39:20
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 why it is behaving the way that it is. Can anyone explain what's happening here? Let’s take a look at

pointer increment and dereference (lvalue required error)

社会主义新天地 提交于 2019-11-27 19:34:14
问题 I am trying to understand how pointer incrementing and dereferencing go together, and I did this to try it out: #include <stdio.h> int main(int argc, char *argv[]) { char *words[] = {"word1","word2"}; printf("%p\n",words); printf("%s\n",*words++); printf("%p\n",words); return 0; } I expected this code to do one of these: First dereference then increase the pointer (printing word1) First dereference then increase the value (printing ord1) Dereference pointer + 1 (printing word2) But compiler

Pre increment vs Post increment in array

本秂侑毒 提交于 2019-11-27 18:50:30
I am learning programming and I have started from C language. I was reading Let us C book. And I was going through this program in that book. main( ) { int a[5] = { 5, 1, 15, 20, 25 } ; int i, j, k = 1, m ; i = ++a[1] ; j = a[1]++ ; m = a[i++] ; printf ( "\n%d %d %d", i, j, m ) ; } My understanding was, it will print i as 2 , j as 1 and m as 15 But somehow it is printing as i as 3 , j as 2 and m as 15 ? Why is it so? Below is my understanding- b = x++; In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5 because old value of ‘x’ is used. b = ++y; In this

Why is “while (i++ < n) {}” significantly slower than “while (++i < n) {}”

不羁的心 提交于 2019-11-27 17:46:58
Apparently on my Windows 8 laptop with HotSpot JDK 1.7.0_45 (with all compiler/VM options set to default), the below loop final int n = Integer.MAX_VALUE; int i = 0; while (++i < n) { } is at least 2 orders of magnitude faster (~10 ms vs. ~5000 ms) than: final int n = Integer.MAX_VALUE; int i = 0; while (i++ < n) { } I happened to notice this problem while writing a loop to evaluate another irrelevant performance issue. And the difference between ++i < n and i++ < n was huge enough to significantly influence the result. If we look at the bytecode, the loop body of the faster version is: iinc

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

非 Y 不嫁゛ 提交于 2019-11-27 17:09:54
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++ ? 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 be made. ++i is slightly more efficient due to its semantics: ++i; // Fetch i, increment it, and return it i++; // Fetch i, copy it, increment i, return copy For int-like

Is ++x more efficient than x++ in Java?

无人久伴 提交于 2019-11-27 15:02:41
During a programming class, the professor was teaching us about x++ and ++x , with x being an integer. He said that in the scenario we are able to just put either x++ or ++x , ++x is more efficient (by little, but still, in theory, more efficient nonetheless). But I forgot why . Anyone knows? This was with Java. zneak It's not more efficient in Java. It can be more efficient in languages where the increment/decrement operators can be overloaded, but otherwise the performance is exactly the same. The difference between x++ and ++x is that x++ returns the value of x before it was incremented,

increment value of int being pointed to by pointer

安稳与你 提交于 2019-11-27 12:38:17
问题 I have an int pointer (i.e., int *count ) that I want to increment the integer being pointed at by using the ++ operator. I thought I would call: *count++; However, I am getting a build warning "expression result unused" . I can: call *count += 1; But, I would like to know how to use the ++ operator as well. Any ideas? 回答1: The ++ has equal precedence with the * and the associativity is right-to-left . See here. It's made even more complex because even though the ++ will be associated with