Unit 17. Introduction to ActionScript (II)

 The operators
 Considering the syntax in detail and the handling of ActionScript, we are going to begin speaking about operators, because of they are the most elementary part of an ActionScript action (and of many other programming languages).   An operator is a character that does an special action inside an ActionScript expression. An expression is nothing more as a set of operators, variables and constants related to each other in a certain way. Flash will take out a result from all the expressions that finds in our movie. For example: x = 3 ; --> It is an expression which results in assigning the value 3 (that's a constant) to the variable 'x' y = 5 + x ; --> It is an expression which results will be to assign to the variable 'y' the sum of the constant 5 and the variable 'x' that is equal 3 (because we have assigned this value to it before). Therefore, the result of this expression is the assignment of the value 8 (3 + 5) to ' y '.   Flash allows us to use many operators, we are going to comment the most common of them. The reader can access the others (and these ones) from the Actions Panel in the folder Operators. We are going to classify the operators as Flash does.   Arithmetic Operators + : Sum. This operator serves, as expected, to add 2 values. - : Substract . It does the operation of subtracting 2 values. * : Multiplication. It multiplies 2 values / : Division. It is the classic operation of division. In contrast with some programming languages, this operator does a complete division (including decimals) % : Remainder Operator . This operator, not very known in mathematics, is a classic of the programming. It returns the remainder between 2 numbers. Example: 4 % 3 = 1, 4 % 2 = 0.   Assignment Operators = : Equal. This is the most important operator of this category and without doubt, one of the most used. It stores the value located in the right side of the expression to the left side. Ex: x = 2 + 3. It stores the value of (2 + 3) in the variable x. Actually, the rest of operators of this category are ways to do several operations at the same time, we'll describe one as example, the others are done exactly in the same way. += : PlusEqual. This operator assigns to the expression located on the left of the operator the resulting value of adding the expression located on the right side with the left side expression. Example: (We suppose that x = 4 e y = 3) then, the expression x += y would cause that x becomes the result of summing (3 + 4). Therefore, the expression x += y is equivalent to doing: x = x + y.   Comparison Operators == : Verify Equality . This operator serves to verify if 2 expressions are equal. If they are, the value of the comparison expression is ' true'. By semantic reasons, say that an expression is true is equivalent to say that it is equal to 1. If they are not equal, it gives back 'false' or the value 0. This possibility of verifying if an expression is equal to another one, will be very useful to verify many items during our movie and depending on them, doing one or others things. We'll put an example; let's imagine that we ask a user to introduce his age in a text field of our flash movie. We call this field "age_user". We make him press a button "Continue" and at this moment we verify his age, if he is 20 years old, we say something to him, otherwise, we say another different thing to him. It would be enough to do something like this:   if (age_user == 20) { give_message_1; } else { give_message_2; }   Here we are saying the following: "If age_user is equal to 20, then give the message 1, otherwise, we give the message 2. We'll see the meaning of 'if' and ‘else’, so don't be concerned about not understanding perfectly the code written above. Logically the creation of the functions "give_message_1" and "give_message_2" is also compulsory.   > : Greater than. It returns true (1) if the expression from the left is greater than the one from the right. Otherwise, it returns false (0). < : Less than. It returns true (1) if the expression from the left is minor to the one from the right. Otherwise, it returns false (0). >= : Greater than or equal . It returns true (1) if the expression from the left is greater or equal to the one from the right. Otherwise, it returns false (0). <= : Less than or equal . It returns true (1) if the expression from the left is minor or equal to the one from the right. Otherwise, it returns false (0). != : Verify Inequality . It returns true (1) if the expression from the left is different from the one from the right. Otherwise, it returns false (0). Example: 3 != 4 would result giving a false (0). So 3 are, indeed, different from 4.   Other Operators ( ) : Parentheses. They serve to group terms and to give preferences in the operations (it is the same as in mathematics). It is also used, as we already saw, to assign parameters to functions or to actions. (These must be between parentheses) " " : Quotes. In ActionScript, all that goes between quotes is considered a string of characters, so that the functions and actions that affect exclusively to the character strings also begin to affect a element between quotes. So for example, while x represents a variable with a determined value, if we write "x", we are writing in fact the character or letter "x". Therefore, we’ll be able to add it to a word, compare it with another letters, write it on the screen etc.. but it will never be a variable.   The rest of operators are not of so wide use, their functionality and definition is explained in the own Flash help.
 Legal warning: Authorised on-line use only. It is not allowed the use of these courses in companies or private teaching centres.