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

Introducing operators

Operator symbols in programming languages represent the arithmetic, assignment, relational, and logical functionality that types can perform. Arithmetic operators represent basic math functions, while assignment operators perform math and assignment functions together on a given value. Relational and logical operators evaluate conditions between multiple values, such as greater than, less than, and equal to.

C# also offers bitwise and miscellaneous operators, but these won't come into play for you until you're well on your way to creating more complex applications. 

At this point, it only makes sense to cover arithmetic and assignment operators, but we'll get to relational and logical functionality when it becomes relevant in the next chapter.

Arithmetic and assignments

You're already familiar with the arithmetic operator symbols from school:

  • + for addition
  • - for subtraction
  • / for pision
  • * for multiplication

C# operators follow the conventional order of operations, that is, evaluating parentheses first, then exponents, then multiplication, then pision, then addition, and finally subtraction (BEDMAS). For instance, the following equations will provide different results, even though they contain the same values and operators:

5 + 4 - 3 / 2 * 1 = 8
5 + (4 - 3) / 2 * 1 = 5

Operators work the same when applied to variables, as they do with literal values. 

Assignment operators can be used as a shorthand replacement for any math operation by using any arithmetic and equals symbol together. For example, if we wanted to multiply a variable, both of the following options would produce the same result:

int currentAge = 32;
currentAge = currentAge * 2;

The second, alternative, way to do this is shown here:

int currentAge = 32;
currentAge *= 2;

The equals symbol is also considered an assignment operator in C#. The other assignment symbols follow the same syntax pattern as our preceding multiplication example: +=, -=, and /= for add and assign, subtract and assign, and pide and assign, respectively.

Strings are a special case when it comes to operators, as they can use the addition symbol to create patchwork text, as follows:

string fullName = "Joe" + "Smith";

This approach tends to produce clunky code, making string interpolation the preferred method for putting together different bits of text in most cases.

With this, we've learned that types have rules that govern what kind of operations and interactions they can have. However, we haven't seen that in practice, so we'll give it a shot in the next section.

Time for action – executing incorrect type operations

Let's do a little experiment: we'll try to multiply our string and float variables together, as we did earlier with our numbers:

If you look in the Console window, you'll see that we've got an error message letting us know that a string and a float can't be added. Whenever you see this type of error, go back and inspect your variable types for incompatibilities:

It's important that we clean up this example, as the compiler won't allow us to run our game at this point. Choose between a pair of backslashes (//) at the beginning of Debug.Log() on line 21, or delete it altogether.

That's as far as we need to go in terms of variables and types for the moment. Be sure to test yourself on this chapter's quiz before moving on!

主站蜘蛛池模板: 诏安县| 临沭县| 株洲市| 绥棱县| 家居| 民勤县| 高台县| 胶南市| 乳山市| 历史| 鲁山县| 昌黎县| 类乌齐县| 陆河县| 磐石市| 枣阳市| 黄浦区| 涿鹿县| 凤庆县| 枝江市| 富民县| 满城县| 洪泽县| 汝城县| 琼结县| 都兰县| 巨鹿县| 巴林右旗| 铜陵市| 上蔡县| 凤翔县| 隆化县| 汽车| 平乡县| 客服| 彭阳县| 临沧市| 建宁县| 和顺县| 罗甸县| 蒲江县|