Mathematical operators in the C # tutorial [original]

Source: Internet
Author: User
There are five simple mathematical operators, two of which have two forms: two yuan and one yuan. the following table lists these operators and uses an example to describe their usage and their results when using simple numeric types (integers and floating-point numbers ).

Result of the example expression of the operator category

 

+ Binary var1 = var2 + var3; the value of var1 is the sum of var2 and var3.

 

-Binary var1 = var2-var3; the value of var1 is the value obtained from var2 minus var3

 

* Binary var1 = var2 * var3; the value of var1 is the product of var2 and var3.

 

/Binary var1 = var2/var3; var1 is the value obtained by dividing var2 by var3.

 

% Binary var1 = var2 % var3; var1 is the remainder of var2 divided by var3

 

+ One dollar var1 = + var2; the value of var1 is equal to the value of var2

 

-One dollar var1 =-var2; the value of var1 is equal to the value of var2, except by-1.

 

Note: The + (mona1) operator is a bit odd because it has no effect on the result. It does not change the value to positive: If var2 is-1, + var2 is still-1. However, this is a universally accepted operator, so it is also included. The most useful aspect of this operator is that it can be customized for its operations. This book will introduce it later when discussing Operator overloading.

 

Xiaotian: In the above example, we assume that var uses a simple numerical type. These types have been practiced N times in the previous example, so we do not need to clarify the examples. But I want to get what results if I add two boolean values together?

 

Laotian: If + (or other mathematical operators) is used for the bool variable, the compiler reports an error. In addition, the addition of char variables is a bit confusing. Because the char variable actually Stores numbers, adding the two char variables together produces a number (whose type is int ). This is an example of implicit conversion.

 

Binary operators + make sense when used for string type variables. As shown below.

 

Result of the example expression of the operator class

 

+ Binary ar1 = var2 + var3; the value of var1 is the connection value of the string stored in var2 and var3.

 

In fact, the examples before string addition are also available, but there are not many examples. Let's look at the two forms below.

 

String str1 = "If inferiority is not helpful to life ,";

 

String str2 = "Try confidence! ";

 

String str3, str4;

 

// The first is the addition of two string variables

 

Str3 = str1 + str2;

 

// The second is the addition of a string variable and a string. The double quotation marks are considered as strings.

 

Str4 = str3 + "the first step of self-confidence is to encourage and recognize yourself from the subconscious. ";

 

Console. WriteLine (str3 );

 

Console. WriteLine (str4 );

 

Console. Read ();

 

Tian: Depressed. Apart from the plus sign (+), other mathematical operators cannot be used for string processing, which makes me waste time doing a half-day exercise.

 

Laotian: You are not doing this. At least you have learned that other mathematical operators cannot be used for string processing in your practice. In addition, you can speed up coding.

 

Next we will introduce the other two operators: increment and decrease operators. They are all unary operators. They can be used in two ways: put before or after the operands. The following table lists the results of simple expressions.

 

Result of the example expression of the operator class

 

++ One dollar var1 = ++ var2; the value of var1 is var2 + 1, and var2 increments by 1.

 

++ One dollar var1 = var2 ++; the value of var1 is var2, and var2 increments by 1.

 

-- One dollar var1 = -- var2; the value of var1 is var2 -- 1, and var2 decreases by 1.

 

-- One dollar var1 = var2 --; the value of var1 is var2, and var2 decreases by 1.

 

These operators change the values stored in the operands.

 

● ++ Always adds 1 to the operand

 

● -- Always subtract 1 from the operand

 

The result stored in var1 is different because the position of the operator determines when it plays a role. Place the operator before the operand, And the operand is affected by the operator before any other calculation, while the operator is placed behind the operand, the operand is affected by the operator after the expression is calculated.

 

This is useful for another example. Consider the following code:

 

Int var1, var2, var3, var4, var5 = 5, var6 = 5, var7 = 5, var8 = 5;

 

Var1 = ++ var5;

 

Var2 = var6 ++;

 

Var3 = -- var7;

 

Var4 = var8 --;

 

Console. WriteLine ("var1 = {0}, var2 = {1}, var3 = {2}, var4 = {3 }"

 

, Var1, var2, var3, var4 );

 

// Overall instance, wondering why it is equal to this result

 

Int v1 = 2, v2 = 3, v3;

 

V3 = v1 ++ * -- v2;

 

Console. WriteLine ("v3 = {0}", v3 );

 

Result 2-26 of the above two examples

 

Figure 2-26

 

Day: It's fresh. The final value is 4. This is because ++ after v1 takes effect only after the current expression. In other words, in this expression, ++ after v1 can be ignored. The -- before v2 takes effect in the current expression, so the effect is v1 = 2, v2 = 3-1, or 2, so it is v1 2 multiplied by v2 2, so of course it is 4.

 

This type of expression has many purposes and is especially suitable for use in loops, which will be explained in the subsequent process control chapter.

 

This article is a new one. For more information, see the source and author!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.