# "C # Learning Note" "4" there are other operations besides subtraction (arithmetic and logical operators)

Source: Internet
Author: User

The content of this section is a bit dull, but very simple, remember, just like in elementary school we just learned subtraction mixed operation. Remember the priority of the operation. (Of course, if you have a C language or other basis, you can skip it, but I don't recommend skipping it)

Arithmetic operators

What is an arithmetic operator? Of course, is to do mathematical arithmetic, such as the subtraction. Here are some common operators (+-*/I will not say).

Take the remainder operator%

% can be used as an operator, the meaning is to take the remainder, also known as the modulo operation. Examples are as follows:

`int a=5; int b=3; int c=a%b;   // that is, 5 is divided by 3 to find the remainder, the result is of course 2. Console.WriteLine (c); // operation result is 2`
Lazy operator +=,-=,*=,/=,%=

These operators are a bit of a miracle, but the meaning is very well understood, I first write an example:

`int 5  22 222; Console.WriteLine (a);`

If I were to tell you here that a+=2 is the equivalent of a=a+2, the result is 7, so what else can you guess?

I think you guessed, and that's why I call these operators lazy operators, and he can simplify the writing of many sentences. Of course, you don't know that these operators won't affect you doing the other, because, he's just shorthand.

Halo operator ++,--

In fact, + + and--it's easy to understand, but they can make you dizzy in some situations.

First of all, the meaning of it, + + is equivalent to +=1, that is, its own increase in 1,--is self-minus 1. For example, the following program:

`int a=0; a+ +;   // that is a=a+1; and a+=1; Console.WriteLine (a); // operation result is 1`

In the example above, I wrote + + on the back of the variable. Read here, is not read out some of the outside sound, is it possible to write in the front of + +? You're right. See the following example:

`int a=0; + +A; Console.WriteLine (a); // operation result is 1`

See here, is not feeling depressed, two kinds of writing and no difference, the results are the same. Then I'll write an example below:

`int 0 ; int b; int 0 ; int  = a++= + +C; Console.WriteLine (a); Console.WriteLine (b); Console.WriteLine (c); Console.WriteLine (d);`

Did you run the code above? If not, you can guess the results first. The results are as follows

Is it not the same as the expected result? The results of our operation, a,b,c,d are 1,0,1,1 respectively. Why is there a situation above? That's what I'm talking about. An operator that makes a person faint. Let me explain in detail below.

If the + + and-operator are in front of the variable, then the variable is self-increment and then participates in the entire operation. If it appears after the variable, then the variable will be calculated first, in the self-increment or self-reduction, combined with the above example analysis.

b=a++ this sentence, since the increment operator appears after the variable A, therefore, the variable a first involved in the operation, that is, b=a such an assignment operation, at this time the a=0, and then A to self-increment 1. There are a=1,b=0 in the results;

D=++c this sentence, the increment operator appears in front of the variable, so the variable is first self-increment, which is c=1, and then assigned to D, so C and D are equal to 1.

Are you still dizzy? Try typing your own code. Arithmetic operators I've introduced so much.

Logical operators (relational operators)

High school math is a simple logical operation, in that part of the collection. Simple logic is the same as or not.

First of all, do you remember what it was that you mentioned earlier? Equality operator = =

= = Equality operator

Described earlier, = = will determine whether the left is equal to the right, if equal, the result is true, if not, the result is fale, such as below.

`BOOL b;b=3= =2; Console.WriteLine (b); b=3= =3; Console.WriteLine (b); // Run result the first is false and the second one is true`

Why do you store results with bool type variables? Think about it, what is the type of variable that holds true and false?

>,<, >=,<=,! = operator

It is said that the first two I have seen, greater than and less than, then after three what is it? That is greater than or equal to, less than equals, not equal to. Because we are not easy to enter the ≤,≥,≠ three symbols on the keyboard. As for the example program, I omitted, as in the previous, change the symbol is OK.

&&,| |,! Operator

The above three are logical and logical OR, logically non, they are used to concatenate the values of two bool types, and give the final operation value, the following table is the operation relationship:

 operator left right results note && true true true All true is true False False False It's fake or fake. False True False True False False || False False False All false or False True True True That's true. False True True True False True ! - True False True change of false - False True It's a false change.

The table above is called the truth tables, here is a simple example:

`BOOL b; int 2 ; int 3 ; int 2  = x > y && x >== x > Y | | X >==! (x > y); Console.WriteLine (b);`

Guess the running results and run the program yourself to verify it!

Ternary operator? ：

This operator is very wonderful, but also a question mark and colon, I will first write an example, say what this:

`int 0 ; int 1 ; int max = a > B? a:b; int min = a < b? a:b; Console.WriteLine (max); Console.WriteLine (min); // run result, first 1, second 0`

If I see some clues, I have found the maximum and minimum values in a and B respectively. So what does this ternary operator do?

Before the question mark is a logical expression (the one with the logical operator), and if the result of this logical expression is true, then the program jumps to? And: Executes between, if the question mark is false before, then jump directly to: Execute later. The algorithm that takes the maximum value in the above program is explained below.

First a>b a logical judgment, if true, that is, A is greater than B, then execute? The back of a, that is max=a, if A>b is false, then the maximum value is B, that is: the back, so max=b.

Just touching this is a bit difficult to understand, we just have to remember that there is such an operator on the line. It will be mentioned again in a later example.

Precedence of operators (precedence rules for blending operations)

The priority levels listed in the following table are high-to-low (for example, multiplication priority is higher than plus and minus)

 Operator Description x++,x-- Post-increment self-reduction +x,-x,!x,++x,--X Plus, minus, non, pre-increment, pre-decrement *，/，% Multiplication, division, take-over +,- Add, Subtract >,<,>=,<= Greater than, less than, greater than or equal to, less than or equal to ==,! = Equal, not Equal &&,| |,? ： Logical AND, logical OR, ternary operators = Assignment operators +=,-=,*=,/=,%= Compound assignment operation

If the operator of the sibling appears, it is evaluated in order from left to right.

Of course, you can use () to change the order of operations, just like the parentheses in our math. Note that parentheses can also be used outside the parentheses, rather than in brackets or curly braces.

In practice, if you do not remember the order of operations, we can add parentheses to ensure that our results are the same as our expected results.

How variables are named (Supplemental to the previous section)

This part should have been written in the previous article, because of fear of excessive content, written in today's content.

If you have learned C language you may have this experience, when declaring variables, a moment, a, and then another B, and then look back, a what is the thing again? So giving a good name to a variable can help us understand the program and unify the code style. Here are two common naming methods. (If you have a better approach or do not use the method, then I did not recruit)

Pascal naming Method (Pascal)

In this way, we capitalize the first letter of each word in the variable, such as username (user name), Studentnumber (school number), is it very intuitive to know the meaning of the variable? Of course, our variables do not use this naming method, which is used in later classes, interfaces, methods, attributes.

Camel naming Style (camels)

The difference between this and the previous one is the first letter of the first word, lowercase, such as username,studentnumber. This is the way we use to name variables.

A valid variable name

Not any character can be used as a variable name. Can only be a letter or underscore (_), followed by any number of letters and numbers and underscores, as to length, you have a variable name to write hundreds of letters can you remember? Also, do not use keywords to do variable names, such as the int,double we have learned in the preceding, will learn some later. Note that C # is strictly case-sensitive, username and username are two variables.

Summarize
1. What does the arithmetic operator refer to, and what are the common
2. + + and--the difference before and after a variable
3. What are logical operators, and what are the
4. Understand the ternary operator, he is the only one
5. Precedence of Operators
6. Supplementary variable naming problem

"C # Learning Note" "4" there are other operations besides subtraction (arithmetic and logical operators)

Related Keywords:
Related Article

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.