(4) Statement form - 3 Boolean data

3. Boolean data

  • The
    legal value of boolean data can only be TRUE or FALSE data.

Operators that use Boolean data defined in C language are divided into two categories:
relational operators and logical operators.


3.1 Relational operators

  • The relational operator (relational operator) is
    used to compare two values.

(1) Compare the relationship between two values
> greater than
<less than
>= greater than or equal to
<= less than

(2) Judging the equality relationship
== equal to
! = not equal

Operator precedence sorting:
judging the equality relationship <comparing the magnitude relationship between two values ​​<arithmetic operator (+ or -)


Common mistakes:
An equal sign is an assignment operator;
relational operators can only be used to compare atomic data values-they can no longer be broken down into smaller parts of data.


3.2 Logical operators

  • The operands of logical operators
    are Boolean values, and they are combined to form another Boolean value.

! Logical negation (if the subsequent operand is FALSE, the value is TRUE).
&& Logical AND (if both operands are TRUE, the value is TRUE).
|| Logical OR (if one of the operands is TRUE, the value is TRUE).


  • De Morgan's Law (DeMorgan law)
    on any logical expression pand q, the following two rules:
    ! (p || q)equivalent to !p && !q
    ! (p && q)equivalent to!p || !q

Common mistake: To
test whether a number is within a certain range, it is not enough to combine them with relational operators.
The two parts of the condition must be clearly &&connected.

(0 < x) && (x < 10)

3.3 Simplified evaluation

  • Simplified evaluation (short-circuit evaluation)

    When the C program is calculating an expression of the form

    exp1 && exp2
    

    or

    exp1 || exp2
    

    When, the sub-expression is always evaluated separately from left to right. Once the result can be determined, the calculation is aborted.
    For example, if the &&expression exp1is FALSE, it need not be calculated exp2, because the result can be determined FALSE.
    The method of stopping the calculation as long as the result is certain is called simplified evaluation.


Advantages: The
first condition can control the execution of the second condition.

For example, to express the following two conditions. First, xthe value of the integer variable is non-zero; second, it xcan be divided evenly y.
As the expression y%xonly xit does not count as only 0, so the test for this condition can be expressed:

(x != 0) && (y % x == 0)

In the above example

(x != 0)

Such a condition test to prevent calculation errors in subsequent parts is called guard.


3.4 Logo

boolType variables have a special name in programming: flags.

For example, declare a boolean variable as follows:

bool done;

The variable donebecomes a sign, which can be used to record whether a certain operation has been completed in the program.


You can assign values ​​to flags just like any other variable. Such as:

done=TRUE;

More importantly, you can assign any Boolean expression to a Boolean variable.

For example, as long as the requirements of the variable itemsRemainingvalue is 0, the end of the specific operation immediately.
Then, you can use the following methods to doneassign appropriate values:

done = (itemsRemaining==0);

Common mistake:
When using Boolean data, avoid redundancy. when

  • Compare a Boolean value with the constant TRUE
  • Use an if statement to produce a Boolean result value

In fact, this value can already be used as a conditional expression.


3.5 Code example

#include <stdio.h>

/* Determine whether it is a leap year. */

main() {
    
    

    int year;
    _Bool isLeapYear;
    printf("Which year? ");
    scanf("%d", &year);

    isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);

    if (isLeapYear) {
    
    
        printf("%d is a leap year.\n", year);
    } else {
    
    
        printf("%d is not a leap year.\n", year);
    };
}


Refer to
"Science and Art of C Language"-Chapter 4 Statement Form

Guess you like

Origin blog.csdn.net/m0_38111466/article/details/108899861