The Logical Operators
In Chapter 4, we learned how to make logical decisions using Boolean values. The decisions we considered were based on single factors: if a movie hasn’t loaded, don’t start playing; if a spaceship has double-shot power, increase the damage its shots inflict, and so on. Not all programming logic is so simple. We’ll often want to consider multiple factors in our branching (i.e., decision-making) logic.
Suppose, for example, that we are making a personalized Flash site that requires users to log in. If a user logs in as a guest, the content he can see is limited. When a registered user logs into the site, we set a variable that indicates, for the duration of our movie, that the user is allowed to see privileged content:
var userStatus = "registered";
To decide whether to allow access to a restricted area, we use a Boolean check like this:
if (userStatus == "registered") { // It's okay, let 'em in... }
Suppose we want to demonstrate the site to prospective investors
without forcing them to register and without showing them material
reserved for registered users. We can invent a new user category,
“specialGuest,” that allows investors to see more than a
guest but not as much as a registered user. When we want to identify
someone as a special guest, we set userStatus
to
“specialGuest.”
How do we perform a Boolean check now that there are two categories of users to allow in? We could do things the brutish, painful way by duplicating portions of our code:
if (userStatus ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.