C # explain the basic content of operators and expressions frequently used in coding,

Source: Internet
Author: User

C # explain the basic content of operators and expressions frequently used in coding,
[Preface]

As the saying goes, it's better to hit the iron, so let's sum up a little bit if you learn something about it.

[Overview]

This summary mainly summarizes the operators, Operation expressions, and some additional content when talking about operators and operation expressions.

[Operator]

The operators are similar to the annotator and variable mentioned above. There are also several types of operators, so the small editor will give you several common operators and operation expressions below.

Type Example
Value assignment operator =
Arithmetic Operators +,-, *,/, And % (remainder)
Compound assignment operator + +,-, + =,-=, * =,/=, % =
Relational operators (Greater than)>, (less than) <, = (equal to, equal ),! = (Unequal), >=( greater than or equal to, inclusive), <=( less than or equal to, inclusive)
Logical operators & (Logical and ),

First, let's talk about the value assignment operator "="

int nums=11;

The value assignment operator assigns the value on the right of the value assignment operator to the variable on the left. The expression connected by the value assignment operator is called the value assignment expression. The above result is to assign the nums value to 11.

Variable values can be assigned repeatedly. Once a new value is attached to a variable, the old value in the variable does not exist.

Next, let's talk about Arithmetic Operators "+ ,-,*,/"

// For example, calculate the number of days (for example, 3 months and 3 days), int day = 3; int month = 3; int sumday = month * 3 + day * 3; int resultday = sumday/7; int result = day % 7; console. writeline ("{0} week {1} Days", resultday, result); console. readkey ();

But one thing that needs to be emphasized in arithmetic expressions is their priority: First multiplication and division, then addition and subtraction, and first calculation in parentheses with parentheses, perform operations at the same level from left to right. Parentheses can be applied without restriction, but must appear in pairs.

Then let's talk about the compound assignment operator "++,-, + =,-=, * =,/=, % = ".

This is often used in the subsequent cycle structure learning, so I will summarize it here. We can clearly see why this is called the composite assignment operator. This compound value assignment operator is actually composed of two arithmetic operators. It only needs one operand to perform operations, so they are also called unary operators. If two operands are required like arithmetic operators, they become binary operators.

Next, let's talk about the simplest "addition and subtraction" in compound operators"

The unary operator ++ and--have a higher priority than the dual operator + or-. They add or subtract one by themselves. At the same time, they also have pre-added minus or post-added minus.

// ------------- 1int num = 10; num = num ++; console. writeline ("num value: {0}", num); console. readkey (); // ------------- 2int num = 10; num = ++ num; console. writeline ("num value: {0}", num); console. readkey ();

The running results of the prefix and Postfix operations are the same, but not all operations are the same. They are very different when involved in the operation. As follows:

// ------------ 1int num = 10; num = 10 + num ++; console. writeline ("num value: {0}", num); console. readkey (); // ------------ 2int num = 10; num = ++ num + 10; console. writeline ("num value: {0}", num); console. readkey ();

If you run the task again, the results will be very different. The first running result is 20, and the second running result is 21.

The latter is to take the original num value for the calculation, and then add 1 to itself, while the former is to add 1 to itself, and then participate in the calculation.

Next, let's talk about this comparison operator ." + \-\*\/"

The comparison operator is relatively simple. It is similar to the Arithmetic Operator in use, except that it returns a Boolean value (true false), which must be received using a Boolean value.

Finally, let's talk about this logical operator "& (logical and), | (logical or ),! (Not logically )"

The following example shows a relationship between them.

[Operation expression]

You must understand his name. An arithmetic expression is actually an expression composed of operators. As the code above shows, it is an operational expression.

[Summary]

These are the foundations that we will often use when writing code in the future. You must be familiar with them and practice them more.

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.