The third day of learning C language: relational operators and logical operators

1. Relational operators

        The expressions used for comparison in C language are called "relational expressions". The operators used in them are called (relational expressions) and are "relational operators" (relational operators). There are mainly the following six.

  • > greater than operator

  • < less than operator

  • >= greater than or equal to operator

  • <= less than or equal to operator

  • == equals operator

  • != inequality operator

        Here are examples:

a > b;

a < b;

a >= b;

a <= b;

a == b;

a != b;

        Relational expressions usually return 0 or 1, indicating true or false.

        In C language, 0 represents false and all non-zero values ​​represent true. For example, 20 > 12 returns 1, 12 > 20 returns 0

        Relational expressions are often used in if or while structures. For example:

if (i == 3)
{
    printf("i is 3.");
}

       Note: The equality operator == and the assignment operator = are two different operators, so do not confuse them. Sometimes, you may accidentally write the following code. It may run, but it is easy to produce unexpected results.

if (i = 3)
{....}

        In the above example, the original intention is i == 3, but it is accidentally written as i = 3. This formula means assigning a value of 3 to variable i, and its return value is 3, so the if judgment is always true.
In order to prevent this kind of error, some programmers like to write variables on the right side of the equal sign.

if (3 == x)
{...}

        If == is mistakenly written as =, the compiler will report an error. Notice.

        There is another mistake to avoid: multiple relational operators should not be used together.

        example:

        i < j < k

        In the above example, two less than operators are used in succession. This is a legal expression and will not report an error, but it usually does not achieve the desired result, that is, it does not guarantee that the value of variable j is between i and k. Because relational operators are evaluated from left to right, the following expression is actually executed.

(i < j ) < k 

        In the above formula, i < j returns or 1, so ultimately or 1 is compared with the variable. If you want to determine whether the value of variable j is between i and k, you should use the following writing method.

i < j && j < k

        Example: Enter an age, if the age is between 18 and 36 years old, output youth        

#include <stdio.h>

int main()
{
	int age = 0;
	scanf("%d", &age);

	if (18 <= age <= 36)
	{
		printf("青年");
	}

	return 0;
}

        When 10 is entered, it is also a youth, as shown in the picture:

        This is because we first compare 18 with the 10 stored in age. The expression 18<=10 is false, and the result of 18<=age is 0. Then we compare 0 with 36. 0<=36 is true, so print Youth, so even when age is 10, youth can be printed. There is a logical problem. How should this code be written? 

#include <stdio.h>

int main()
{
	int age = 0;
	scanf("%d", &age);

	if (age <= 18 && age <= 36)
	{
		printf("青年");
	}

	return 0;
}

2. Logical operators

        Logical operators provide logical judgment functions and are used to construct more complex expressions. There are mainly three operators:

  • ( ! ): Logical negation operator (changes the true or false value of a single expression)

  • (&&): AND operator, which means AND (the expressions on both sides are true, then it is true, otherwise it is false)

  • ( || ): OR operator, which means or (if at least one expression on both sides is true, it is true, otherwise it is false)

            

 

         Note: In C language, non-0 means true and 0 means false.

        2.1. Logical negation operator

                For example, we have a variable called flag. If flag is false and we want to do something, we can write the code like this:

if(!flag)
{
  printf("do study");  
}

                If flag is true, !flag is false. If flag is false, !flag is true. So the meaning of the above code is that if flag is false, execute the code in the if statement.

        2.2.AND operator

                && is the AND operator, which also means AND. && is a binary operator, and the method used is a&&b. When the expressions on both sides of && are true, the entire expression is true. As long as one of them is false, then The entire expression is false.
For example: If we say that the month is March to May, which is spring, how do we use code to reflect it?

#include <stdio.h>

int month = 0;

scanf("%d", &month);

if(month >= 3 && month <= 5)
{
    printf("春季\n");
}

        The meaning expressed here is that month must be greater than or equal to 3 and less than or equal to 5, and must be satisfied at the same time.

        2.3.OR operator

               || is the OR operator, which means or. (||) is also a binary operator. The method used is (a || b). As long as one of the expressions on both sides is true, the entire expression is true. , it is false only when the expressions on both sides are false.

For example: We say that the month of the year is December or January or February is winter, so how do we use code to reflect it?

#include <stdio.h>

int month = 0;

scanf("%d", &mont);

if(month == 12 || month == 1 || month == 2)
{
    printf("冬季\n");
}

         Practice using leap years:

                Enter a year, year, to determine whether year is a leap year.

                1. A leap year is divisible by 4 and not divisible by 100.

                2. If it is evenly divisible by 400, it is a leap year.

#include <stdio.h>

int main()
{
    int year = 0;

    scanf("%d", &year);


    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        printf("%d是闰年!", year);
    }

    return 0;
}

        operation result:

Guess you like

Origin blog.csdn.net/m0_58724783/article/details/132101941