Logical (IF), relational, numeric, and conditional Operators

Source: Internet
Author: User

If-else if (a = 0) {1} else if (a> 0) {2} else {3} features: 1, 2, 3 only one bracket will be executed Trap 1, if (! = 0) {A is not 0;} else {A is 0;} error if (a); {} if (a = 0) should be a = 0; If (10) int A = 9; should be if (10) {int A = 9;} scope problems logical operators note:

& -- One off is false.

| -- True: int I = 0; Int J = 0; If (++ I> 0) | (++ j> 0 )) {printf ("% d, % d \ n", I, j);} I = 1 in the output result, j = 0 l if you want to determine whether the value of A is within the range of (3, 5), do not write it as 3 <A <5, because the Union direction of Relational operators is "from left to right ". For example, if a is 2, it calculates 3 <A, that is, 3 <2. The condition is not true and the result is 0. Compare with 5, that is, 0 <5. The condition is true and the result is 1. Therefore, the result of 3 <A <5 is 1 and the condition is true. That is to say, when the value of A is 2, the value of a is within the range of (3, 5. This is obviously incorrect. The correct judgment method is: (A> 3) & (A <5) l c language: any non-0 value is true, and only 0 is false ". Therefore, logic and values are also applicable to numerical values. For example, the result of 5 & 4 is 1, which is "true"; the result of-6 & 0 is 0, which is "false" 1. Division and modulo Operators

-7/4 =-1-7./4 =-1.75-7% 4 =-3

7/4 = 1 7./4 = 1.75 7% 4 = 3 1> remainder operation: Positive and Negative are determined by the value on the left of %.

2> c99 requires the Division operator to use "Zero tail truncation ". We recommend that you convert-1.75 to-1 instead of-2.

3> the modulo operator is only used for integer operations and is invalid for floating point numbers. According to the division of integers in c99, the rule "toward Zero truncation tail" is specified. If the first operand is negative, the modulo obtained after the modulo operation is also negative. If the first operand is an integer, the resulting modulo is also an integer. Relationship between remainder and modulus: A % B = A-(A/B) * B 2. incremental (++) subtraction (--) Operator

When N ++ is a part of the expression, it means "use n first, then increase its value by 1"; similarly, + + N indicates "first increase N value by 1 and then use it ".

If a variable appears in multiple parameters of the same function, do not use the increment or decrement operator on it.

When a variable appears in an expression multiple times, do not apply the increment or decrement operator to it.

The incremental operator can only be used before the variable name, but not before the constant.

3. type conversion(1) When an expression appears, both the signed and unsigned char and short types are automatically converted to int. If necessary, it will be automatically converted to unsigned int (if the short and INT have the same size, the unsigned short is larger than the int; in that case, convert unsigned short to unsigned INT ).

(2) In any operation that contains two data types, both values are converted into two types with higher levels.

(3) The sequence of data types from high to low is Long Double, double, float, unsigned long, long, unsigned long, long, unsigned int, and Int. One possible exception is that when long and INT have the same size, the unsigned int is higher than long.

(4) In the value assignment statement, the final result of the calculation is converted to the type of the variable to be assigned. Upgrade or downgrade.

(5) When passed as a function parameter, char and short will be converted to int, and float will be converted to double.

Level Improvement is usually a smooth and harmless process, but downgrading may lead to real problems. The reason is simple: a lower-level type may not be large enough to store a complete number. Automatic type conversion should be avoided, especially downgrading.

 

If statement: If (condition expression) {Statement 1; Statement 2 ;...} If the condition is true, execute the statement in braces. Otherwise, skip the statement and go down. NOTE: If (conditional expression); {Statement 1; Statement 2 ;...} Because the if statement has braces, The if condition expression in the braces is executed regardless of whether the expression is true or false. For example, if (! = 0) is equivalent to the if (a) conditional expression? Expression 1: expression 2 if the condition expression is true, the value of the whole expression is expression 1 if the condition expression is false, the value of the whole expression is expression 2 For example: int c = A> B? A: B;

 

Logical (IF), relational, numeric, and conditional 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.