We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Can someone help me with example 5.8 from the book Make: Getting Started with Processing (page 54)? I don't understand the use of variable x in the following line of code:
x += (targetX - x) * easing;
x is declared as a global variable of type float at the beginning of the sketch, but never initialized before this line. My question is: how can x be used inside its own initialization (targetX - x)? What is the value of x in this expression when it hasn't been assigned one at this point? Is Processing using a default value of 0.0?
Thank you in advance.
Answers
https://Processing.org/reference/addassign.html
http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
@jsilver -- you are correct, java has default initializations (e.g. int 0; String null, boolean false) for global (not local) variables. The compiler sets float x to 0.0f before the addassign.
Don't think of the addassign as a self-initialization -- think of it as an assignment with an implicit default initialization that already happened.
Thank you GoToLoop. I had no problem with the += operator, but your second link was a useful reminder. Thank you jeremydouglass for such an informative and clear explanation. And yes, you're right about the default being "0.0f" as it would otherwise default to a double.