ID:166192
Sep 7 2006, 10:29 am
|
|
I'm just curious what ! does...
|
Here are common two situations for its use:
1. In DM, null, 0, and "" are actually different values. Therefore, when you write if(x == 0), you're only checking if x is equal to 0. If x is equal to null or "" there, unless you really mean to check for 0, you might not get the results you're expecting (especially since variables equal null by default if they aren't initialized to another value). The statement if(!x) is equivalent to if(x == null || x == 0 || x == "") (likewise, if(x) is equivalent to if(x != null && x != 0 && x != "")). In this case, ! can be read as "not." This use of the ! operator is extremely important because it prevents hard-to-track bugs. 2. In programming, certain variables are classified under the category of boolean, meaning that they will either be equal to 1 or to 0 at all times. If it helps, think of a light switch as a boolean variable: if the light switch is in the on position, it signifies that the light is on; if the light switch is in the off position, it signifies the opposite. With boolean variables, 1 signifies "on" and 0 signifies "off." When applied to a boolean variable x, the ! can be read as "set x to the opposite of its current value," or going back to the light-switch analogy, "flip the switch." That means that if x is "on" (is equal to 1), the statement !x will change turn it off (equal to 0). Likewise, if it is "off" and the statement !x occurs, it will then be "on." The ! variable in this context can make certain writings much more concise. Say we want to make a verb fly that enables its user to become non-dense, and therefore able to go through other objects. Also, the user needs to be able to land. Without or knowledge of the ! operator as applied to boolean variables, we might write something like this: mob/verb/Fly() However, before we go with that, let's take a closer look at atom.density. When it's equal to 1, the atom is dense and cannot pass through other dense atoms. When it's equal to 0, the atom is non-dense and can pass through aforementioned dense objects. No other valid values but 1 and 0? It's a boolean variable! In that case, we can shorten it up: mob/verb/Fly() As you can see, the ! operator as applied to boolean variables can shorten and clean up our code. |
In response to DarkCampainger
|
|
DarkCampainger wrote:
You can also use it for "Does not equal": > if(Text != Password) //If the variable Text is not equal to the variable Password Nope. != is actually an operator of its own. =P |
So it's real use comes mostly in conditions, e.g. if() statements:
You can also use it for "Does not equal":
I suppose you could think of it as "Toggle" for true/false.