03C # BASICS (2 ),

Source: Internet
Author: User

03C # BASICS (2 ),

1. Comparison Operators

= Equals ;! = Not equal to;> greater than; >=greater than or equal to; <less than; <= less than or equal;

Comparison operators (also known as Relational operators) are used to judge the authenticity of values. The result is of the bool type;

 

2. Operator priority

The operator has a priority. The operator has a higher priority ";

The great "()" has the highest priority;

 

3. Value of the value assignment expression

The value assignment expression itself is a value, which is the value of the variable on the left after the value assignment;

 

4. I ++ and ++ I

I ++ is auto-incrementing after being assigned a value, and ++ I is auto-incrementing before being assigned a value;

I -- and -- I are the same;

 

5. logical operators

Logical operators are mainly used to calculate boolean values, including: & (and/and); | (OR );! (Not );
& Binary operator. The result is true only when the expressions or values on both sides are true. Otherwise, all values are false;
| Binary operator. If one of them is true, the result is true;
! : Unary operator, returns the inverse value, which is false or false;

 

Short circuit operation

& Operation: when the first value is false, the entire operation result must be false, so there is no need to calculate the second value;

| Operation: this is also the case. If the previous result is true, it must be true, and there is no need to calculate the result;

! There is no short circuit involved in the calculation;

 

6. Ternary Operators
Syntax: conditional expression? Expression 1: expression 2

If the conditional expression is true, the value of the expression is "expression 1", otherwise it is "expression 2 ";

 

7. if statement
Syntax:

1 if (comparison expression) 2 {3 // code block to be executed 4}

If the comparison expression is true, execute the statement in braces

If the if statement is followed by only one statement, it can be abbreviated as "ignore braces (not recommended)". Even if there is only one sentence, braces should be written;

1 if (comparison expression) 2 Console. WriteLine ("Demo code"); // code block to be executed;

The following code:

1 if (comparison expression) 2 Statement 1; 3 Statement 2; 4 5 // during compiler compilation, we will add braces to the following code 6 7 if (comparison expression) 8 {9 Statement 1; 10} 11 Statement 2;

 

Else clause

If statements can be followed by else

1 if (comparison expression) 2 {3 // code block executed when the comparison expression is true 4} 5 else6 {7 // code block executed when the comparison expression is false 8}

 

Else if

If can also contain else if

1 if (condition expression 1) 2 {3 // code 4} 5 else if (condition expression 2) to be executed when condition expression 1 is true) 6 {7 // code 8} 9 else if (condition expression 3) to be executed when condition expression 2 is true) 10 {11 // The condition expression 3 is true, which is the code to be executed. 12}

Execution Process: first judge the first expression. If it is true, execute the statement block in the braces it carries. Otherwise, judge whether the next conditional expression is true, and so on.

 

8. switch-case

Syntax:

1 switch (expression) 2 {3 case values: 1: 4 // statement; 5 break; 6 case values: 2: 7 // statement; 8 break; 9... 10 default: 11 // statement; 12 break; 13}

The expression can be byte, short, int, char, string, or enumeration type;
Break indicates the end of the switch. default is equivalent to the else of the if statement. if all case statements do not match, the default statement is executed;

Merge multiple switch conditions: if the code to be executed by multiple case conditions is the same, merge the statements and add a break;

 

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.