Expressions and operators (C #)

Source: Internet
Author: User

Most expressions use operators, which combine one or more operands to form an expression and return the operation result.

1. Expression

An expression is composed of operators and operands. Below are some simple expressions:

Int I = 556 // declare an int type variable I and initialize it to 556i = I * I-11 // change the value of variable I
2. Operators

An operator is a special symbol used to process data operations. The following are common operators in C.

2.1 Arithmetic Operators


+,-, *,/, And % operators are called arithmetic operators. They perform addition, subtraction, multiplication, division, and remainder Operations respectively.

2.1.1 addition Operators

The standard addition operation is performed by adding two numbers.

Sample Code:

Staticvoid main (string [] ARGs) {int M1 = 999; // declare the integer variable M1 and assign the value to 999 int m2; // declare the integer variable m2 = m1 + 1; // The value of M2 is the value after the sum of M1 and 1. writeline (m2.tostring); console. read ();}
2.1.2 subtraction Operator

The standard subtraction operation is performed by subtracting the value of another expression from one expression.

Sample Code:

M2 = M1-1; // m2 is the value after M1 and 1 Subtraction
2.1.3 multiplication Operator

Use two expressions to perform the multiplication (*) operation and return the product of them.

Sample Code:

Sum = is1 * is2 // make the sum value the product of is1 and is2
2.1.4 division Operator

Run (/) to obtain the operator by dividing the divisor expression by the divisor expression.

Sample Code:

Is = shi1/shi2 // set the is value to shi1 divided by shi2.

 

2.1.5 return the remainder Operator

Returns the remainder of the Division.

2.2 value assignment operator

Assign operators to assign new values to variables, attributes, events, and other elements. There are several major assignment operators:

 

Name

Operator

Operation Rules

Meaning

Assignment

=

Assign an expression to a variable

Give the value on the right to the left

Add Value

+ =

X + = y

X = x + y

Subtraction

-=

X-= y

X = x-y

Multiplication and assignment

* =

X * = y

X = x * y

Division assignment

/=

X/= y

X = x/y

Modulo assignment

% =

X % = y

X = x % Y

Assign value

& =

X & = y

X = x & Y

Shift value to the right

> =

X> = y

X = x> Y

Left shift assignment

<=

X <=y

X = x <Y

XOR assignment

=

X = y

X = xy

Sample Code:

Static void main (string [] ARGs) {int I = 999; // declare the integer variable I and assign the value to 999 I + = 1; // use the console OF THE plus value assignment operator. writeline ("last I value: {0}", I); console. readline ();}

 


The final running result of the program is: 1000

2.3 Relational operators


As the name implies, Relational operators can be used to compare two values. After an operation, a Boolean value representing the operation result is returned. Common Relational operators include:

 

Relational operators

Description

Relational operators

Description

= (Different from VB)

Equal

! =

Not equal

> 〉

Greater

> =

Greater than or equal

<〈

Less

<=

Less than or equal

Relational operators are generally used in judgment or loop statements.


Sample Code:

Int M1 = 111; // declare the integer variable M1 int m2 = 222; // declare the integer variable m2 bool result; // declare the bool variable result = m1 = m2; // make result equal to M1 and M2 and perform the return value console. writeline (result); console. readline ();

 


The program running result is false.

2.4 logical operators (this blog will introduce them in detail)

Logical operators perform Boolean logical operations on two expressions. C # Sino-German logical operators can be divided into bitwise logical operators and Boolean logical operators ".

2.4.1 bitwise logical operators


Including the bitwise "and" operator, bitwise "or" operator, and bitwise "XOR" operator.

2.4.1 Boolean logical operators

Including the Boolean "and" operator, Boolean "or" operator, and Boolean "XOR" operator.

2.5 Shift Operator

It mainly includes the Left shift operator and the right shift operator.


The Left shift operator (<<) can be used to shift the number to the left, which means that all bits are moved to the left for a specified number of times. The high sub-position will be lost, and the low position will be filled with zero.


The right shift operator is similar to the Left shift operator.

2.6 other special Operators

For example, the is operator, conditional operator, new operator, and typeof operator. If you are interested, you can learn more.

3. Operator priority

Generally, an expression contains more than one operator. How can the program determine the sequence of operators? This utilizes the priority of operators. Operators with higher priority are executed first. It shows in detail the priority of operators:

 

Category

Operator

Priority

Basic

X. y, f (x), a [X], X ++, X-, new,
Typeof, checked, unchecked

By

High

To

Low

RMB 1

+,-, And ,-,! ,~ , ++, --, (T) x

Multiplication and division

*,/, %

Addition and subtraction

+ ,-

Shift

<<,> 〈〈,〉〉

Comparison

<<=, <=, Is,

Equal

= ,! =

Bitwise AND

&

Bitwise OR

Bit or

|

Logic and

&&

Logic or

|

Condition

? :

Assignment

=, + =,-=,/=, * =, % =, & =, Etc.

 

The above is my introduction to expressions and operators in C #. I hope it will help you ~

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.