At 2/24/13 08:35 PM, cheese123 wrote:
The for loop seemed unnecessary ... I just needed the while loop
You know that the only significant difference between a while loop and a for loop is syntax, right?
They're both loops and can break your application if used incorrectly.
At 2/24/13 08:35 PM, cheese123 wrote:
And ok maybe I don't understand if statements 100% (if (x = y) or if (x ==y) - why are these two different) but I understand them well enough to do what I want.
If you don't understand the difference between the assignment operator and the equality operator then you can't know them well enough to be able to use them to do what you want. They are very different operators and cannot be used interchangeably.
I will, however, provide an answer your question in that part of the post: why are these two different?:
The first argument you provided, (x = y), is assigning the value of "y" to "x" and then returning the value of "y". If you were to put that into an if statement it would always be evaluated as true if "y" is any non-zero value (e.g. a literal 0, false, an empty string, et cetera). In AS3 whenever you assign a value to a variable the value that was assigned will be returned; this means that you can use an assignment in an if statement and the value that is assigned is what will be evaluated in the statement.
The second argument, (x == y), is the equality operator. As the name suggests it determines if two given values are to be considered equal. This is generally the operator you will want to be using in your if statements, opposed to the assignment operator. There are circumstances where you will want to use the assignment operator in an if statement, typically as part of a loop, but I won't go into that since it's not something you're likely to need to do.
And for the sake of learning I'll touch on an operator you did not mention, which is the identity operator. It is three equals signs that looks like (x === y). It is used to determine if "x" and "y" are the exact same object, or, in other words, are identical. It not only determines that they are equal, but, essentially, that they are the occupy same piece of memory as well. Before showing what this operator does it is worth clarifying that in AS3 that pretty much all, if not all, variables you use are references. Sometimes you might want to determine if two variables reference the same piece of memory, even if it's possible that the two variables are referencing separate, but equal, memory locations. That's where the identity operator is used.
Take the following code for example:
var foo:Thing = new Thing(42);
var bar:Thing = foo;
var baz:Thing = new Thing(42);
trace(foo === bar); // Output: true
trace(foo === baz) // Output: false;
"foo" and "bar" are both references to the same memory, and "baz" is a reference to a separate piece of memory, so that is why when you use you identity operator on "foo" and "baz" you get a false result, because the two memory locations are not identical, even though they contain equal data; the memory locations themselves are what differ which is what is important with the identity operator.