官术网_书友最值得收藏!

Time for action – Using logical operators

  1. Let's put our first example in code.
    var bool bRaining;
    var float CurrentMoney, RequiredMoney;
    
    function PostBeginPlay()
    {
        'log(!bRaining && CurrentMoney > RequiredMoney);
    }
    
    defaultproperties
    {
        CurrentMoney=20.0
        RequiredMoney=15.0
    }

    Remembering that bools are false by default, let's take a look at the log:

    [0007.94] ScriptLog: True

    Even though bRaining is False, we're asking the code if it's NOT raining, which is True. You can see why naming booleans is important now. If our variable were called bNotRaining , working with logical operators would get messy pretty quickly.

  2. Let's look at our second example.
    var string Day;
    
    function PostBeginPlay()
    {
        'log(Day == "Tuesday" || Day == "Thursday");
    }
    
    defaultproperties
    {
        Day="Monday"
    }

    Since the day variable is neither of those two, we'll get False in the log:

    [0007.79] ScriptLog: False
  3. One final operator to discuss is the EXCLUSIVE OR, denoted by two carets ( ^^ ). This will return true if one and only one of our statements is true. Let's look at the following code:
    var string Day, Month;
    
    function PostBeginPlay()
    {
        'log(Day == "Tuesday" ^^ Month == "January");
    }
    
    defaultproperties
    {
        Day="Tuesday"
        Month="January"
    }

    If we were using a normal OR, this would return true, but since both of them are true, an EXCLUSIVE OR returns false:

    [0007.60] ScriptLog: False

What just happened?

We can see how intertwined logical operators are with normal comparisons. We used the equal and greater than comparisons in our examples. When working with them, the best way to figure out how to write a statement is to say it out loud first and take note of words like AND, NOT, and OR in your sentence. This will help you figure out how to construct a logical operator statement.

Have a go hero – Writing logical statements

How would you write the following statement with logical operators?

If the sun is shining and we're not wearing sunblock...…

Answer: The operator would look something like this:

if(bSunShining && !bWearingSunblock)

Concatenation

Concatenation is a fancy word for "join two strings together". There are two concatenation operators, let's take a look at them.

主站蜘蛛池模板: 万山特区| 乌拉特前旗| 若尔盖县| 民权县| 类乌齐县| 衡阳市| 星子县| 如东县| 行唐县| 武胜县| 淅川县| 苗栗县| 崇义县| 靖远县| 万山特区| 黄冈市| 云林县| 廉江市| 克东县| 新丰县| 阿勒泰市| 镇赉县| 邓州市| 资溪县| 旬邑县| 乌拉特前旗| 阳朔县| 五常市| 寻乌县| 化州市| 兴仁县| 乌鲁木齐市| 阿拉善盟| 营口市| 寿阳县| 辽阳县| 称多县| 台中市| 沿河| 班玛县| 林州市|