"Learning Notes", "C language" logical operators

Source: Internet
Author: User
Tags arithmetic operators

Sometimes, we need to set up a number of conditions at the same time to execute a piece of code, such as: the user only input QQ and password, to execute the login code, if only entered QQ or only entered a password, you can not execute the login code. In this case, we are going to use the logical operators provided by the C language.

The result of a logical operation is only 2: "True" is 1, "false" is 0

&& Logic and

1> using formats

"Condition a && condition B"

2> operation Results

Only if both condition A and condition B are established, the result is 1, which is true, and the rest is 0, or false. Therefore, if a condition A or condition B is not tenable, the result is 0, that is, "false"

3> Operation Process

Always first determine if condition A is established

L If condition A is established, then determine if condition B is established: If condition B is established, the result of "condition a && condition B" is 1, i.e. "true", if condition B is not established, the result is 0, i.e. "false"

If condition A is not established, it will not be judged whether the condition B is established: Since condition A is not established, the result of condition a && condition B must be 0, that is, "false", regardless of condition B.

4> Example

The combination of logic and direction is "from left to right". such as Expressions (a>3) && (a<5)

If the value of a is 4: First Judge A>3, set up, and then Judge A<5, also established. So the result is 1.

If the value of a is 2: First Judge A>3, not set up, stop judging. So the result is 0.

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

5> Note

If you want to determine if the value of a is within the range of (3, 5), you must not write 3<a<5, because the relationship operator is "left to right". For example A is 2, it will first calculate 3<a, that is, 3<2, the condition is not set, the result is 0. Compared with 5, namely 0<5, the condition is set up, the result is 1. Therefore, the result of 3<a<5 is 1, the condition is established, that is, when a is a value of 2 o'clock, the value of a is within the range of (3, 5). This is obviously wrong. The correct way to judge is: (a>3) && (a<5)

LC Language Provisions: Any non-0 value is "true", only 0 is "false". Therefore logic and also apply to numerical values. For example, 5 && 4 result is 1, "true", -6 && 0 result is 0, "false"

2.| | Logical OR

1> using formats

"Condition A | | Condition B "

2> operation Results

When condition A or condition B is set up (also including condition A and condition B), the result is 1, which is true, and only if condition A and condition B are not set, the result is 0, which is "false".

3> Operation Process

Always first determine if condition A is established

If condition A is established, it will not be judged whether the condition B is established: Because condition A has been established, regardless of condition B, "condition A | | The result of condition B must be 1, which is "true."

If condition A is not established, then determine if condition B is true: If condition B is established, "condition A | | Condition B "results in 1, that is," true ", if condition B is not set, the result is 0, i.e." false "

4> Example

The logical or binding direction is "from left to right". such as expressions (a<3) | | (a>5)

If the value of a is 4: First Judge A<3, not set up, and then Judge A>5, also does not establish. So the result is 0.

If the value of a is 2: First Judge A<3, set up, stop judging. So the result is 1.

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

5> Note

C language provisions: Any non-0 value is "true", only 0 is "false". So logic or also applies to numeric values. such as 5 | | 4 The result is 1, for "true";-6 | | 0 The result is 1, for "true"; 0 | | The result of 0 is 0, which is "false"

3.! Logical Non-

1> using formats

“! Condition a "

2> operation Results

In fact, the condition A is reversed: if condition A is established, the result is 0, that is, "false"; if condition A is not established, the result is 1, that is, "true". That is to say: true false, false become true.

3> Example

The logical non-union direction is "from right to left". Like an expression! (a>5)

If the value of a is 6: First Judge A>5, set up, and then reverse the result is 0

If the value of a is 2: First Judge A>3, not set, and then take the result of the reverse is 1

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

4> Note

Logical non-operators can be used multiple times:! (4>2) The result is 0, is "false",!! (4>2) The result is 1, is "true",!!! (4>2) The result is 0, which is "false"

C language provisions: Any non-0 value is "true", only 0 is "false". Therefore, a non-0 value is not logical! The result of the operation is 0, the value of 0 is logical non! The result of the operation is 1.! 5,!6.7 、!-9 results are 0,!0 results of 1

4. Priority level

The order of precedence for logical operators is: parentheses () > minus sign->! > Arithmetic operators > Relational operators > && > | |

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

Expression 3+2<5| | 6>3 equivalent to ((3+2) < 5) | | (6>3) with a result of 1

Expression 4>3 &&!-5>2 equivalent to (4>3) && ((! ( -5)) > 2) with a result of 0

1#include <stdio.h>2 3 intMain ()4 {5     //logic and Conditions 1 && conditions 26     7     //int a = 10>3 && 7<6;8     9     //int a = 0 &&;Ten      One     //printf ("a=%d\n", a); A      -     /* - int a = ten; the int b = ten; -      - //int C = (a>5) && (++b>=11); -      + int c = (a<5) && (++b>=11); -      + //a = ten A //b = Ten at //c = 0 - printf ("a=%d, b=%d, c=%d\n", A, B, c); -     */ -      -      -     //logic or Condition 1 | | condition 2 in      -     //int a = 0 | | to     /* + int a = ten; - int b = ten; the      * int c = (a<5) | | (b++-ten); $     Panax Notoginseng //a = ten - //b = One the //c = 0 + printf ("a=%d, b=%d, c=%d\n", A, B, c); A     */ the      +     //logical non! condition -     //returns 0 if the condition is true, and returns 1 if the condition is not true $      $     //int a =! (10>8); -      -     //int a =!-10; the      -     //int a =!10>8;Wuyi      the     /* - int a =!! Ten; Wu      - printf ("a=%d\n", a); About     */ $     return 0; -}

"Learning Notes", "C language" logical operators

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.