Vous êtes sur la page 1sur 2

Let's focus on our first logical operator. We know how to compare things, which is great.

For example, if the user tries to buy something in the store, we just compare the user's money to the cost of the item. But what if we want an object where the user can only buy the object if they have enough money AND only if their level is high enough? We can use the operator and! Right now, we are learning something that I'm just calling logic, but what other people call BOOLEAN ALGEBRA. It's actually kind of fun. Here's what we are going to do: We are going to look at a statement made up of pieces, and use those pieces to determine if the whole statement is true or false. For example, look at the statement below. I will eat if I have food and if I am hungry. How do you know if I will eat? Well, two pieces need to be true for me to eat. First, I have to have food, obviously. But, even if I have food, I'm not going to eat unless I'm hungry. If I'm hungry but I don't have any food, then obviously I'm not going to eat. So, both pieces need to be true for the entire statement to be true. I'll only eat if I am hungry, and if I have food. Okay, so we know that an A AND B statement is only true if both A, and B, are independently both true. If either A, B, or both are false, then the statement A and B is false. Let's do some practice. Look at the statement, and determine if it's value by typing true or false. 5 + 5 == 10 and 6 > 4 This time, let's use actual code. A NEW SYMBOL: When we program in flash, instead of using the word 'and' to compare two statements, we use the symbol && Is the statement in the parenthesis true or false? 1 private var userMoney:uint = 10; 2 private var userLevel:uint = 6; 3 private var ITEM_COST:uint = 5; 4 private var ITEM_LEVEL:uint = 10; (userMoney >= ITEM_COST && userLevel >= ITEM_LEVEL)

A NEW SYMBOL: When we program in flash, instead of using the word 'or' to compare two statements, we use the symbol || Is the statement in the parenthesis true or false? 1 private var a:uint = 10; 2 private var b:uint = 6; 3 private var c:uint = 5; 4 private var d:uint = 12; (a + b == c + d || a*b == c*d) The third logical operator should be familiar. An exclamation point (!) means not. We saw it earlier when we used !=. If ! is in front of a statement, the truth value of that statement switches. True becomes false and false becomes true. !true = false !( !true ) = true 1 private var numberOne:Number = 15; 2 3 private var numberTwo:Number = 16; 4 numberOne++; numberTwo == numberOne true

Vous aimerez peut-être aussi