Dark Horse programmer----arithmetic operations, relational operations, logical operations

Source: Internet
Author: User

first, arithmetic arithmeticA total of 34 operators are available in the 1.C Language 2. Basic operations (add, subtract, multiply, divide) 3. Take remainder operation (also called modulo operation)
    • % both are integers, and if a decimal number will be an error
    • The positive and negative of the result of the remainder depends on the integer on the left
    • The remainder operation can pin the value of an integer to how much
4. Automatic Type Changer
int 10.8 // double is automatically converted to int, there will be a warning;
5. Force type to replace
int a = (int)10.8//  Double is automatically converted to int, no more warnings;
6. Automatic type Promotion
Double 10.6 6 // The 6 is automatically promoted to double for the addition operation;
7. Composite operations
5 // A = a + 5;  6//  a = a * 6;  564//  a = A + (5 + 6 + 4);
8. Code Exercise

Second, relational operations (comparison operations)1. Conditional judgment
    • There is no bool type in C language;
    • The C language stipulates that any data is true and false: Any value not 0 is real, only 0 is false;
2. Priority level
    • The precedence of the = =,! = in the relational operator equals,<, <=, >, >=, and the precedence of the former is lower than the second;

such as: int result = 2 > 3 = = 1;

Parse: First operation: 2 > 3 return 0, False

Re-operation: 0 = = 1; Returns to 0, False

That results: result = 0;

    • The relationship operator is bound from left to right;

such as: int result = 4 > 3 > 2;

Result: result = 1 > 2 = 0;

    • The precedence of the relational operator is less than the arithmetic operator

such as: int result = 3 + 4 > 8-2;

Parse: int result = 7 > 6;

That results: result = 1;

third, logical operation1. There are only two results of logical operations: TRUE and false; 2. There are three operators:
    • Logic and: Format: Condition 1 && Condition 2;
    • Logical OR: Format: Condition 1 | | Condition 2;
    • Logical non: Format:! condition;
3. Note the point:
    • When the result of condition 1 has been able to determine the overall result, the condition 2 is no longer running

Such as:

int a = 10;

int B = 10;

int C = (A < 5) && (++b >= 11);

Result: A = ten, B = ten, c = 0;

Parsing: When the operation ends with a < 5, which is 0, it will decide C = 0; b >= 11 will no longer be run, i.e., no ++b, resulting in B = 10.

    • The condition can also be a constant;

such as: int c = 5 && 6;

    • Logic can not be ligatures;

such as: int a =!!  5; Result: a = 1;

4. Code Exercise:

Results:

four or three mesh operation1. Format:
    • Conditions? Value 1: value 2;

such as: int a = ten > 5? 9:6;

Result: a = 9;

2. Determine the size of two numbers
int Ten ; int 9 ; int c = a > B? A:B;
3. Determine the size of three numbers
int Ten ; int 9 ; int 998 ; int Abmax = (a > B)? a:b; int max = (Abmax > C)? Abmax:c; // Result: max = 998;
4. Code Exercise

Operation Result:

Dark Horse programmer----arithmetic operations, relational operations, logical operations

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.