[Study Notes] [C Language] logical operators, learning notes logical operators

Source: Internet
Author: User

[Study Notes] [C Language] logical operators, learning notes logical operators

Sometimes, we need to execute a certain piece of code when multiple conditions are set up at the same time. For example, a user can execute the login code only after entering the QQ and password at the same time, if you only enter QQ or password, you cannot execute the logon code. In this case, we need to use the logical operators provided by the C language.

There are only two logical operations: "true" is 1, and "false" is 0.

& Logic and

1> Format

"Condition A & Condition B"

2> operation result

Only when Condition A and Condition B are set, the result is 1, that is, "True". In other cases, the result is 0, that is, "false ". Therefore, if either Condition A or condition B is invalid, the result is 0, which is false"

3> operation process

Always first judge whether Condition A is true

L if condition A is true, then judge whether Condition B is true. If Condition B is true, the result of Condition A & Condition B is 1, that is, true ", if Condition B is not true, the result is 0, that is, "false"

L if condition A is not true, it will not judge whether Condition B is true: Because Condition A is no longer true, regardless of Condition B, the result of "Condition A & Condition B" must be 0, that is, "false"

4> example

The combination direction of logic and is "from left to right ". For example, expression (a> 3) & (a <5)

If the value of a is 4: First Judge a> 3, then judge a <5, also true. Therefore, the result is 1.

If the value of a is 2: Judge a> 3 first. If not, stop the judgment. Therefore, the result is 0.

Therefore, if the value of a is within the range (3, 5), the result is 1. Otherwise, the result is 0.

5> note

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)

LC language rules: 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"

2. | logical or

1> Format

"Condition A | Condition B"

2> operation result

If Condition A or condition B is set to true (both Condition A and Condition B are set), the result is 1, that is, true "; if neither Condition A nor Condition B is true, the result is 0, which is false ".

3> operation process

Always first judge whether Condition A is true

If Condition A is true, Condition B will not be judged whether it is true. Because Condition A is true, regardless of Condition B, the result of "Condition A | Condition B" must be 1, that is, "true"

If Condition A is not true, then judge whether Condition B is true. If Condition B is true, the result of Condition A | Condition B is 1, that is, true ", if Condition B is not true, the result is 0, that is, "false"

4> example

The logic or combination direction is "from left to right ". For example, expression (a <3) | (a> 5)

If the value of a is 4: first judge a <3, not true; then judge a> 5, not true. Therefore, the result is 0.

If the value of a is 2: first judge a <3, true, Stop Judgment. Therefore, the result is 1.

Therefore, if the value of a is within the range of (-∞, 3) or (5, + ∞), the result is 1. Otherwise, the result is 0.

5> note

C language: any non-0 value is true, and only 0 is false ". Therefore, it is logical or suitable for numeric values. For example, the result of 5 | 4 is 1, which is "true"; the result of-6 | 0 is 1, which is "true"; the result of 0 | 0 is 0, false"

3 .! Non-logical

1> Format

"! Condition"

2> operation result

In fact, Condition A is reversed: If Condition A is true, the result is 0, that is, "false". If Condition A is not true, the result is 1, that is, "true ". That is to say, it is true or false.

3> example

The combination direction of logic is "from right to left ". For example, expression! (A> 5)

If the value of a is 6: First Judge a> 5, then it is true, and then the inverse result is 0.

If the value of a is 2: Judge a> 3 first. If it is not true, the result after the inverse is 1.

Therefore, if the value of a is greater than 5, the result is 0. Otherwise, the result is 1.

4> note

You can use the logical non-operator for multiple consecutive times :! (4> 2) the result is 0, which is "false ",!! (4> 2) the result is 1, which is "true ",!!! (4> 2) the result is 0, which is false"

C language: any non-0 value is true, and only 0 is false ". Therefore, perform logical non-0 values! The calculation results are all 0, and the logic of the 0 value is not! The calculation result is 1 .! 5 ,! 6.7 ,! -The result of 9 is 0 ,! The result of 0 is 1.

4. Priority

The priority of logical operators is: parentheses ()> minus sign->! > Arithmetic Operators> Relational operators >&>||

Expression! (3> 5) | (2 <4) & (6 <1): Calculate first! (3> 5), (2 <4), (6 <1), the result is 1, and the formula becomes 1 | 1 & 0, and then 1 & 0, formula 1 | 0. The final result is 1.

Expression 3 + 2 <5 | 6> 3 is equivalent to (3 + 2) <5) | (6> 3). The result is 1.

Expression 4> 3 &&! -5> 2 is equivalent to (4> 3 )&&((! (-5)> 2), the result is 0.

 

1 #include <stdio.h>
 2 
 3 int main ()
 4 {
 5 // Logical AND Condition 1 && Condition 2
 6
 7 // int a = 10> 3 && 7 <6;
 8     
 9 // int a = 0 && 10;
10
11 // printf ("a =% d \ n", a);
12
13 / *
14 int a = 10;
15 int b = 10;
16
17 // int c = (a> 5) && (++ b> = 11);
18
19 int c = (a <5) && (++ b> = 11);
20
21 // a = 10
22 // b = 10
23 // c = 0
24 printf ("a =% d, b =% d, c =% d \ n", a, b, c);
25 * /
26
27
28 // logical OR condition 1 || condition 2
29
30 // int a = 0 || 11;
31 / *
32 int a = 10;
33 int b = 10;
34
35 int c = (a <5) || (b ++-10);
36
37 // a = 10
38 // b = 11
39 // c = 0
40 printf ("a =% d, b =% d, c =% d \ n", a, b, c);
41 * /
42
43 // Logical NOT! Condition
44 // If the condition is true, return 0; if the condition is not true, return 1
45
46 // int a =! (10> 8);
47
48 // int a =! -10;
49
50 // int a =! 10> 8;
51
52 / *
53 int a = !! 10;
54
55 printf ("a =% d \ n", a);
56 * /
57 return 0;
58} 

 


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.