C # common Operators

Source: Internet
Author: User

After learning about C # data types and variables, we will also encounter operations between variables, such as calculating the sum of two numbers. Which of the following commonly used operators are available in C )? So... Let's take a look!

I. Arithmetic Operators

1. Basic Arithmetic Operators

When it comes to arithmetic operators, we should first be able to think of some common operators in our mathematics, such as: addition, subtraction, multiplication, division, and remainder!

In C #, how do these operators represent them?

Operator name

C #Operator number

Addition

+

Subtraction

-

Multiplication

*

Division

/

Remainder

%

How can we use C # after the expression of basic operators? Let's look at the SectionCodeLet's see what an expression is.

1 int A = 10; // define an integer variable a value of 10 2 int B = 20; // define an integer variable B value as 20 3 4 // define a variable C value as variable A + B 5 Int c = a + B; // code segment expressed by a group of operators operator 6 7 // define a variable C1 value as variable a1-b1 8 int C1 = A-B; 9 10 // define a variable C2 value as variable A2 * B2 11 int C2 = A * B; 12 13 // define a variable C3 value as A3/B3 14 int C3 = A/B; 15 16 // define the C4 value of a variable as A4 % B4 17 int C4 = A % B; 18 19 console. writeline ("A + B = {0}", c); 20 console. writeline ("a-B = {0}", C1); 21 console. writeline ("a * B = {0}", C2); 22 console. writeline ("A/B = {0}", C3); 23 console. writeline ("A % B = {0}", C4 );

In the code above, we mainly discuss two knowledge points: the use of basic operator addition and what is an expression. Let's analyze it.

Let's look at the fifth line of code. in C #, it is very simple to use the basic arithmetic operator. Just like our mathematical addition, first we can see that (A + B) is equivalent to (10 + 20) after the calculation is complete, the calculation result is assigned to the variable on the left of the equal sign. In this way, a basic arithmetic addition operation is completed, and the code combination of the Code Description of an addition operation is performed, we can see it as an expression in C.

After the analysis, we will all be able to launch the product? Let's take a look at the running results.

Are the results expected?

2. Auto-increment and auto-increment Operators

After learning about the basic operators, let's look at a very useful OPERATOR: the auto-increment operation is entrusted with the auto-subtraction Operator!

Operator name

C #Operator number

Auto-Increment

++

Auto increment/decrease

--

So what do auto-incrementing symbols and auto-subtraction operators mean? Read the Code:

1 int A = 1; 2 A ++; // A = a + 1 3 int B = A; 4 --; // A = A-1 5 Int c = A; 6 7 console. writeline ("A ++: {0}", B); 8 console. writeline ("A --: {0}", C );

Okay, let's analyze it:

First, let's look at the second line A ++; the auto-incrementing symbol ++ is equal to itself adding 1, then a = 1, after executing a ++ (A itself adding 1 ), a's own value is 2

So let's look at the fourth line A --; the self-Recommendation symbol -- is equal to itself minus 1, then a is equal to 2 at this time. After executing a -- (A minus 1), a's own value is 1.

After analysis, let's verify the answer!

Ii. assignment operators

After reading the basic arithmetic operators, let's take a look at what is the value assignment operator. In fact, we have used the value assignment operator in the above example. What is the value assignment operator, by the way, it is the variable value symbol, that is, the equal sign in the above example.

So is the assignment operator only having one equal sign? The answer is wrong. C # also provides some operators to Simplify expression replication, as shown in the following table:

Operator name

C #Operator number

Equal sign assignment

=

Equal sign assignment

+ =

Equal sign assignment

-=

Multiplication equal sign assignment

* =

Evaluate equal sign assignment

% =

So what do these equal signs mean? In fact, this is a simple answer. Let's look at a piece of code and compare it with the above example. We will find that these value assignment operators help us simplify the expressions.

1 int A = 10; 2 A = a + 10; // assign the value of A to a (10) + 10 3 // after the operation is complete, A = 20 4 console. writeline ("A = a + 10; A = {0}", a); 5 6 A = 10; // restore a back to 10 7 A + = 10; // equivalent to a = a + 10; assign the value of A to a (10) + 10 8 // after the operation is complete, A is equal to 20 9 console. writeline ("A + = 10; A = {0}", );

Let's take a look:

You can see all lines 1 to 4, so you can see 7 rows directly. In fact, A + = 10 is equivalent to a = a + 10, but this operator simplifies the expression. Let's prove it with facts.

 

3.. Operator priority and bracketsApplication

After the operator and expression, let's look at the priority between operators. What is the priority? In fact, the priority is the operator that should be executed when multiple operators appear in our expression. It is like the first multiplication, division, and addition and subtraction in mathematics. in C #, the first multiplication, division, and addition and subtraction are no exception. I am not going to introduce more priorities here, because we can use parentheses to indicate the priority of expressions (). For more information about operator priority, see C # related books. Let's use an example to describe the priority of addition, subtraction, multiplication, and division in C!

1 int A = 10*2-10/2 + 3; // The result is 18 2 Console Based on the first multiplication, division, and addition and subtraction principle. writeline ("10*2-10/2 + 3 Results: {0}", );

Let's analyze it. Based on the rule of first multiplication, division, and addition and subtraction:

First, calculate 10*2 and the result is 20.

In calculation 10/2, the result is 5.

In this case, Expression Programming is 20-5 + 3. here we need to note that addition and subtraction belong to the same level operator.

At this time, the result of 20-5 execution is 15.

After performing 15 + 3, the result is 18 and the facts are used to prove my analysis.

Well, what if I want 10x3 to divide 2 + 3 to 10 at this time? Don't know? The Code tells you:

1 int A = 10*3-10/(2 + 3); // at this time, the operator priority in () is the highest. Therefore, execute the expression 2 console in parentheses first. writeline ("10*3-10/(2 + 3) Result: {0}", );

Let's continue with the analysis. Based on the rule with the highest priority of parentheses, we can see:

First, the result of execution (2 + 3) is 5.

After 10*3 is executed, the result is 30.

In this case, the expression is 30-10/5, and the Division expression is 30-2.

The result is 28. Check the truth!

 

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.