40. My C # learning note 6

Source: Internet
Author: User
Tags mathematical functions

An expression:

An expression is made up of operators and operands. operator to set what operations are performed on the operand. For example:+,-,*,/ are operators, operands include literals, constants, variables, and expressions.

Operator:

Operators are special symbols that are used primarily for mathematical functions, some types of assignment statements, and logical comparisons. C # provides rich operators such as arithmetic operators, assignment operators, comparison operators, and so on.

Arithmetic operators:

The +,-,*,/,and% operators are called arithmetic operators, respectively, for addition, subtraction, multiplication, In addition to and to find the remainder of the operation.

The "+" and "-" operators can also be used as positive and negative symbols of the data.

The addition operator (+) performs a standard addition operation by adding two numbers.

For example: Create a console application, declare two integer type variables M1 and M2, and assign M1 to 927, the value of M2 is then added to the value of M1 and M1 . The code is as follows:

public static void Main (string[] args)

{

int m1=927;

int m2=m1+m1;

Console.WriteLine (M2. ToString ());

Console.readkey ();

}


If you want to add 1 to the integer variable M , you can do so with "m=m+1;" It can also be implemented with the increment operator (+ +). if:m++ or ++m. ++m is the prefix increment operation, the result of which is the value after the operand plus 1 ;m++ is the suffix increment operation. The result of this operation is the value before the operand increases.


subtraction operator (

Create a console application that declares two decimal r1 r2 1112.82 and 9270.81 decimal Span style= "Font-family:consolas;" >R3 r2 minus r1 the resulting value. The code is as follows:

public static void Main (string[] args)

{

Decimal r1=1112.82;

Decimal r2=9270.81;

Decimal r3=r2-r1;

Console.WriteLine (R3. ToString ());

Console.readkey ();

}


If you want to reduce the integer variable R by 1 , you can do so with "r=r-1". It can also be implemented with the decrement operator (--). such as:r-- or --r. --r is the prefix decrement operation, the result of which is the value ofthe operand minus 1 , and ther-- is the suffix decrement operation. The result of this operation is the value before the operand is reduced.


Multiplication operators:

The multiplication operator (*) multiplies two expressions and returns their product.

For example:

Create a console application that declares two integer variables ls1 and ls2, and assigns a value of ten and a respectively. Then declare a variable sum so that its value is the product of LS1 and ls2 .

The code is as follows:

public static void Main (string[] args)

{

int LS1;

int LS2;

ls1=10;

ls2=20;

int sum=ls1*ls2;

Console.tostring (sum. ToString ());

Console.readkey ();

}


The division operator (/) performs an arithmetic divide operation, which is obtained by dividing the dividend expression by the divisor expression.

For example:

Create a console application that declares two integer variables shj1 and shj2, and assigns values of three and 5 respectively . Then declare an integer variable , ls, so that its value is shj1 divided by shj2 . The code is as follows:

public static void Main (string[] args)

{

int shj1=45;

int shj2=5;

int ls=shj1/shj2;

Console.WriteLine (LS. ToString ());

Console.readkey ();

}


Note: When arithmetic operators (+,-,*,/) are used, The resulting result may exceed the range of values of the value type involved, which can result in incorrect running results.


To find the remainder operator:

The redundancy (%) operator returns the remainder of the dividend after dividing the divisor, usually using this operator to create an equation of the remainder within a specific range.

For example:

Create a console application that declares two integer variables I1 and I2, and assigns a value of three and tenrespectively. Declare an integer variable I3so that its value is I2 and I2 after the remainder operation. The code is as follows:

public static void Main (string[] args)

{

int i1=55;

int i2=10;

int I3;

I3=i1%i2;

Console.WriteLine (I3. ToString ());

Console.readkey ();

}


The DivRem method of the Math class can also be used to obtain the remainder of two numbers . As I3=i1%i2 in the above code can be written as Math.divrem (I1,i2,out I3), i3z stores I2 and The remainder of the I2.


assignment operator: The assignment operator assigns a new value to an element, such as a variable, property, event, and so on. The assignment operators are mainly: = " += -= *= /= &=&NBSP; |= ^= <<= >>=

The left operand of an assignment operator must be an expression of a variable, property access, indexer access, or event access type, and if the type of operand on either side of the assignment operator is inconsistent, then the type conversion is required and then the value is assigned.

When using the assignment operator, the type to which the right-operand expression belongs must be implicitly convertible to the type to which the left operand belongs. Operation assigns the value of the right operand to the variable, property, or indexer element of the left operand.


This article is from the "Smile" blog, please be sure to keep this source http://yiyiweixiao.blog.51cto.com/2476874/1977018

40. My C # learning note 6

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.