static int method(int i){
return i++;
}
This method is doing post-increment, which means it will return the value of i
as it is which is being passed as the method argument, after that it increments the value of i
which is now not useful at all since the scope of the variable i is only up to this method.
Try doing pre-increment instead.
return ++i;
EDIT: As per OP's comment
@Prashant by i++ it works, but why i = method(i) cant?
for(i= 10;i<=50; i++){ <-- Here post increment happens
System.out.println(i); <-- At this point value is already being incremented so you can utilize it.
}
Where as
for(i= 10;i<=50; i=method(i)){ <-- Here post increment happens inside method and the value it returns is still `10` not `11`
System.out.println(i); <-- At this point value is same as the previous loop
}