-3

i have a question because i don't find any document that can confirm this: i am taking an example of ++ operator but it can be the same for the other.

first for-loop: LOOP

for(int i=0 ; i < 5; ) {
  i = i++; 
  System.out.println("Hello World");
}

second for-loop: 5 iteration

for(int i=0 ; i < 5; i++) {
  i = i++; 
  System.out.println("Hello World");
}

My question is : the operator ++ is acting in one way when we asign it to other variable ??? and it act in another way when we use it inside a for-loop ?

Rules to excute:

for(int i=0; i < 5 ; i++) {

  //some code here to excetute
}

order of execution:

  • int i= 0
  • i < 5
  • //execute the code in the body
  • operator of update ( i++)

Thanks to all.

Junior Fulcrum
  • 153
  • 1
  • 1
  • 7

2 Answers2

2

Here's an example:

int x = 5;
int y = x++;

The result at this point is y = 5 and x = 6.

Why?

x++ is a post-increment operator: x itself is incremented, but the value of the expression (think return value of sorts) is the original value.

In contrast, ++x is a pre-increment operator: x itself is incremented and the value of the expression is the resulting value. Hence if you do y = ++x instead, y and x will result in the same value.

antak
  • 19,481
  • 9
  • 72
  • 80
-1

i just found the response in this article:

http://introcs.cs.princeton.edu/java/11precedence/

for(int i=0; i<5;)
  i = i++;
}

it is a post-increment operator so the Associativity is Left-To-Right. i++ in it's left has an the operator = . Cause of Associativity ( and not the level of Precedence ) i assign the value of the i in the right to the variable i in the left. After doing this i do the increment but it doest have a reference variable whent o go for the moment so it remain in the memory for the next row of code that will call the i variable.

Precedence of operator:

hightl level of precedence is ++
lower level of precedence is =

Associativity of the operator:

Left to Right for the operator ++
Right to left for the operator =

So: java will read all the line with the text till when java will found the semicolon (;). After finding a semicolon (;) java will call this a statement and will go for first to understand the operator and the variable that has inside. Java see that it has 2 operators , one is ++ and the other one is =. Cause of precedence the operator ++ will decide the associativity.

So the operator ++ has the associativity from Left to Right. So:

  i++  (Left to Right)    ---------------->  (execute everything before i for first)
  ++1  ( Right to Left )  <---------------   (execute everything after i for first)

i++ will calculate first what will be in the left and later it will go in the right.

  • i++ will execute everything in the left first (.. i)++ till i found another operator with hight precedenze that will change the direction of the flow cause of level precedence.
  • The next operator is = and it is with lower level of precedence so i will execute what the operator ++ was saying. For this reason i valutate ( i = i ).
  • After assign the ( i = i ) i dont have other operator and i will turn back the controlo to the operator ++ so i will execute the remain. For this reason i increment of 1 value the i variable.
Junior Fulcrum
  • 153
  • 1
  • 1
  • 7
  • To all the people that is continuing to do -1 for my question you are pleased to not read this post and to go away. The question that i have done is in one of the exam for Oracle Certification. It has to do with the operator , level of Precedence and Association of operators. With my question i was trying to understand something by myself and maybe trying to help some other people. To all the people that is puting -1 please live a comment so we can have a discution about the argument and i can understand my wrong in the excersise. Best regards – Junior Fulcrum Oct 30 '15 at 18:51
  • I think the question is fine, as any C++ programmer will confirm that this is not trivial, but I downvoted the answer because I think it is confusing and maybe even partially incorrect. Some of the sentences don't make a lot of sense, it would help if you (had someone) proofread it and clarified. Something like `++1` is not even legal and `i = i` is never evaluated. In contrast, the other answer (which I upvoted) very concisely answers the question as you asked it, the only thing that might be added, as you noted in a comment, is that it works because the `=` operator has the lowest precedence – CompuChip Nov 02 '15 at 14:33