A logical operator:
&&: Logic and, Read and
Expressions are true on both sides of the expression, so the result is true.
Formula: a false or False
|| : logical OR, read or
Expression the left and right sides, there is a true, then the result is true
Formula: One true is true
!: Logical not, read as reverse
If the result of an expression is false, it becomes true, and if it is true, it becomes false.
Formula: True change false, false change true
Short-circuit problem of two logical operators
Tips: Not 0 is true, 0 is False
Short-Circuit Condition:
&&: Left If False, the right short circuit (right will not be executed)
|| : If true on left, short to right (not on right)
Tips: Code examples are as follows
#include <stdio.h>
int main (int argc, const char * argv[]) {
int res = 1 && 2;
printf ("res=%d\n", res);
int res = 1 &&-2;
printf ("res=%d\n", res);//1
int res = 1 && 0;
printf ("res=%d\n", res);//0
int num1=10,num2=20;
Or the formula: a true true, so if the left is true, then the right expression will not be executed
int res = num1++ | | num2++;
printf ("res=%d num1=%d num2=%d\n", res,num1,num2);//1 11 20
int num1=0,num2=10;
And the formula: a false is false
int res = num1++ && num2++;
printf ("res=%d num1=%d num2=%d\n", res,num1,num2); 0 1 10
int num1=0,num2=10;
int sum = num1++ + num2; 10
int num = 10;
int res = 0 && num++;
printf ("res=%d num=%d\n", res,num); 0 10
int num1=0,num2=10;
And the formula: a false is false
int res = NUM1 && num2++;
printf ("res=%d num1=%d num2=%d\n", res,num1,num2); 1 1 11
int num1=-3,num2=3,num3=4;
int res = (NUM1 + num2) && (NUM1-NUM3);
printf ("res=%d\n", res);//0
int num1=-3,num2=3,num3=4;
int res = (NUM1 + num2) && num3++;
printf ("res=%d num3=%d\n", res,num3);//0 4
int num1=-3,num2=3,num3=4;
int res = num1++ + num2++ && num3++;
printf ("res=%d num1=%d num2=%d num3=%d\n", res,num1,num2,num3);//0-2 4 4
return 0;
}
Logical operator of C language