(original) C # learning note 03--variables and expressions 04--expressions 01--mathematical operators

Source: Internet
Author: User

3.4 Expressions

By combining variables and literals (which are called operands when using operators) with operators, you can create an expression, which is the basic component of the calculation.

This chapter mainly introduces the mathematical and assignment operators, and the logical operators are introduced in the 4th chapter, which mainly discusses the Boolean logic of the control program flow.

The operators are broadly divided into the following 3 classes.

A unary operator that handles an operand;

A binary operator that handles two operands;

Ternary operator, processing three operands;

Most operators are two-tuple operators, with only a few unary operators and a ternary operator, the conditional operator (the conditional operator is a logical operator, as described in chapter 4th). The following is a mathematical operator that includes a unary operator and a two-tuple operation.

3.4.1 Mathematical operators

There are 5 simple mathematical operators, two of which are two and one. Table 3-6 lists these operators and uses a short example to illustrate their usage, as well as their results when using simple numeric types (integers and floating-point numbers).

  Note: the + (unary) operator is a bit odd because it has no effect on the results. It does not turn the value into positive: If VAR2 is-1, then +VAR2 is still-1. However, this is a universally recognized operator, so it is also included in it.
The most useful aspect of this operator is the ability to customize its operation, which is described later in this book, which discusses the overloading of operators.

If you use + (or other mathematical operators) for the bool variable, the compiler will error.

The addition of char variables can also be a bit confusing. Keep in mind that char variables actually store numbers, so adding two char variables together also gets a number (its type is int). This is an example of an implicit conversion, which will be described in more detail later in this topic and explicit conversions, as it can also be applied to cases where var1, var2, and var3 are mixed types.

Binary operator + is also meaningful when used with string type variables. The table entry for table 3-7 should now look like the following.

However, other mathematical operators cannot be used to process strings.

The other two operators that should be described here are increment and decrement operators, which are unary operators that can be used in two ways: placed before or after the operand. The result of a simple expression is shown in table 3-8.

These operators change the values that are stored in the operand.
? + + always adds 1 to the operand
? --always reduce the number of operands by 1

The results stored in VAR1 are different because the position of the operator determines when it will work. Put the operator before the operand, the operand is affected by the operator before any other computations are made, and the operator is placed after the operand, and the operand is affected by the operator after the completion of the evaluation of the expression.

Looking at a simple example, the difference between the operator before and after the operand is realized as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacech03ex01 {classProgra {Static voidMain (string[] args) {            intI, J, M, N; I= j = m = n =3; intK =0; K= ++i; Console.WriteLine ("k = ++i; K is {0}, I am {1}", K, i); K=0;//InitK = j + +; Console.WriteLine ("k = j + +; K is {0}, J is {1}", K, J); K=0;//InitK =--m; Console.WriteLine ("k =--m; K is {0}, M-is {1}", K, M); K=0;//InitK = n--; Console.WriteLine ("k = n--; K is {0}, N is {1}", K, N);        Console.readkey (); }    }}

Operation Result:

As can be seen from the above example, the prefix is : the operand is processed first (in the example: I,m;), and then the operand processing result is returned to the expression (in the example: Return to K), as follows:

    

The suffix is : The value of the operand is first returned to the expression (in the example: returned to K), and then the operand itself is processed (in the example: J, N), as follows:

    

    

The following example shows how to use mathematical operators and describes two other useful concepts. The code prompts the user to type a string and two numbers, and then displays the result of the calculation, as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacech03ex02{classProgram {Static voidMain (string[] args) {            DoubleDfirstnumber, Dsecondnumber; stringstrUserName; Console.WriteLine ("Enter your name:"); strUserName=Console.ReadLine (); Console.WriteLine ("welcom {0}!", strUserName); Console.WriteLine ("Now give me a number:"); Dfirstnumber=convert.todouble (Console.ReadLine ()); Console.WriteLine ("Now give me another number:"); Dsecondnumber=convert.todouble (Console.ReadLine ()); Console.WriteLine ("The sum of {0} and (1) is {2}.", Dfirstnumber, Dsecondnumber, Dfirstnumber+dsecondnumber); Console.WriteLine ("The result of subtracting of {0} from (1) is {2}.", Dsecondnumber, Dfirstnumber, Dfirstnumber-dsecondnumber); Console.WriteLine ("The product of {0} and (1) is {2}.", Dfirstnumber, Dsecondnumber, Dfirstnumber*dsecondnumber); Console.WriteLine ("The resule of dividing {0} by (1) is {2}.", Dfirstnumber, Dsecondnumber, Dfirstnumber/dsecondnumber); Console.WriteLine ("The remainder after dividing {0} by (1) is {2}.", Dfirstnumber, Dsecondnumber, Dfirstnumber%dsecondnumber);        Console.readkey (); }    }}

Run the program and enter the relevant content with the following results:

  Description of the example

In addition to demonstrating mathematical operators, this code introduces two important concepts that will be used many times in future examples.
? User input
? Type conversions

The user input uses a syntax similar to the previous Console.WriteLine () command. But here Console.ReadLine () is used. This command prompts the user to enter information and stores them in a string variable.

string strUserName;             " Enter your name: "  );             = console.readline ();             " welcom {0}! ", strUserName);

This code directly writes the contents of the assigned variable username to the screen.

This example also reads two numbers, which are discussed slightly below. Because the Console.ReadLine () command generates a string, and we want to get a number, this introduces the problem of type conversion.

Dfirstnumber == convert.todouble (Console.ReadLine ());

The string to Console.ReadLine () is used by the command convert.todouble (), the string is converted to a double type, and the value is assigned to the previously declared variable.

Note that in the output, we provide an expression Firstnumber + Secondnumber, etc., as a parameter to the Console.WriteLine () statement, without using an intermediate variable . This syntax can improve the readability of your code and reduce the amount of code you need to write.

" The sum of {0} and (1) is {2}. "                  + dsecondnumber);

(original) C # learning note 03--variables and expressions 04--expressions 01--mathematical operators

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.