[Note] C # basic introduction -- Arithmetic Operators of C,

Source: Internet
Author: User

[Note] C # basic introduction -- Arithmetic Operators of C,

Add: +.The plus sign has two purposes: When the plus sign is used to connect two numbers, the sum of the two numbers is calculated. For example:

Console. WriteLine (9 + 2.2); // output 11.2

In another case, when both sides of the plus sign contain strings, the expressions on both sides are connected to new strings. For example:

Console. WriteLine (9 + "2.2"); // output 92.2

Because "2.2" is a string, 9 is also converted to "9", and + serves as the connection string.

Minus :-.Subtraction is used for subtraction. For example:

Console. WriteLine (15-23); // output-8

Multiplication :*.Multiplication is used to obtain the product of the number 2. For example:

Console. WriteLine (0.8*3); // output 2.4

Except :/. Division number is used to calculate the operator of division of two numbers. For example:

Console. WriteLine (2/0. 5); // output 4.0

However, if two integers are separated, only the integer part is retained and the fractional part is removed.

Console. WriteLine (5/10); // output 0

 

Remainder Operator-- The remainder operator in C # Is%.

The Division operator is used to calculate the operator for division of two numbers, while the remainder operator % is used to calculate the remainder of division of two numbers. For example:

Console. writeLine (19/5); // calculates the 19 operator divided by 5 and outputs 3 console. writeLine (19% 5); // calculate the remainder of 19 divided by 5, and output 4 (more than 3 4)

In programming, % is often used to check whether a number can be divisible by another number. For example, the following code snippet:

Int number = 29; Console. WriteLine (number % 2); // calculates the remainder of number divided by 2

If the output is 0, there is no remainder, that is, the number can be divisible by 2 (an even number); If the output is 1, there is a remainder, that is, the number cannot be divisible by 2 (an odd number ).

 

Operator ++ and --

 

++, CalledAuto-increment operator. For example, if you are 18 years old and you are one year old next year, write the code as follows:

Int age = 18; // 18 years old this year age = age + 1; // next year, add 1 year old to this year's age

You can also write it as follows:

Int age = 18; // age + + 18 this year; // next year, add 1 year to this year's age

Age ++; the same role as age = age + 1 is that of variable + 1.

 

--, CalledAuto-subtraction Operator. Similarly, if you are 18 years old, you will be 17 years old next year after using XX Xiaoshui. You can write as follows:

Int age = 18; // age 18 this year --; // equivalent to age = age-1;

In addition, age ++; and age --; can also write ++ age; or -- age;

 

However, pleaseNote:If ++ is written before or after a variable in the same statement as other operations, see the following example:

Console. WriteLine (age ++); is equivalent to the following two sentences:

Console. WriteLine (age); // print age = age + 1 first; // then add

Console. WriteLine (++ age); is equivalent to the following two sentences:

Age = age + 1; // first add Console. WriteLine (age); // then print

The output results will not be the same if the operation order is different.

The above is taken from MOOC course C # getting started with development

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.