logical operators
A logical operator that combines an expression containing a relational operator to form a new expression; The result is either true or false, and the result value is stored with the BOOL type variable.
Operator |
Explain |
Combination method |
() []->. |
Parentheses (functions, etc.), array, two struct members access |
From left to right |
! ~ ++ -- + - * & ( type ) sizeof |
negation, bitwise negation, increment, decrement, plus sign, Indirect, take address, type conversion, find size |
From right to left |
* / % |
Multiply, divide, take modulo |
From left to right |
+ - |
Plus, minus |
From left to right |
<< >> |
Move left, move right |
From left to right |
< <= >= > |
Less than, less than equal, greater than or equal, greater than |
From left to right |
== != |
Equal to, not equal to |
From left to right |
& |
Bitwise AND |
From left to right |
^ |
Per-bitwise XOR OR |
From left to right |
| |
by bit or |
From left to right |
&& |
Logic and |
From left to right |
|| |
Logical OR |
From left to right |
? : |
Conditions |
From right to left |
= += -= *= /= &= ^= |= <<= >>= |
Various assignments |
From right to left |
, |
Commas (order) |
From left to right |
How to use:
Expression 1 logical operator Expression 2
Logic and: a >= b && a > 5 The result of an expression is false if the expression is true when both sides of the operator are true, and when an expression is false.
int a = 5;
int b = 6;
if (a >= b && a > 5)
{
printf (the expression is true \ n);
}
else
{
printf ("The expression is fake \ n");
}
int a = 5;
int b = 6;
if (a >= b && a > 5)
{
printf (the expression is true \ n);
}
else
{
printf ("The expression is fake \ n");
}
You can try changing the values of A and B to see the results.
Logic or: a >= B | | A > 5 only one of the results is true, and this expression is true.
int a = 6;
int b = 7;
if (a >= B | | a > 5)
{
printf ("True for Expression");
}
else
{
printf ("The expression is fake \ n");
}
int a = 6;
int b = 7;
if (a >= B | | a > 5)
{
printf ("True for Expression");
}
else
{
printf ("The expression is fake \ n");
}
A >= B is false, but a > 5 is true, so it prints: the expression is true.
Logical Non:! A, if a is true, then!a is false, if a is false, then!a is true.
BOOL flag = YES;
printf ("!flag =%d\n",!flag);
printf ("flag =%d\n", flag);
BOOL flag = YES;
printf ("!flag =%d\n",!flag);
printf ("flag =%d\n", flag);
First line printout:!flag = 0
Second line printout: flag = 1
Short-circuit of logical operators
Logic and short circuit:&& left is false, the right does not participate in the operation.
For example:
int a = 6;
int b = 0;
if (a >= && b = 5)
{
printf ("The value of the expression is true \ n");
}
printf ("B =%d\n", b);
int a = 6;
int b = 0;
if (a >= && b = 5)
{
printf ("The value of the expression is true \ n");
}
printf ("B =%d\n", b);
The output of B here is: 0, because in expression a >= && B = 5, a >= 6 is true, so the right side B = 5 does not participate in the operation, so B is equal to the original 0.
Short Circuit of logic or: | | The left is true and the right-hand side does not participate in the operation.
For example:
int a = 6;
int b = 0;
if (a >= 6 | | b = 5)
{
printf ("The value of the expression is true \ n");
}
printf ("B =%d\n", b);
int a = 6;
int b = 0;
if (a >= 6 | | b = 5)
{
printf ("The value of the expression is true \ n");
}
printf ("B =%d\n", b);
The output of B here is: 0, because in expression a >= 6 | | b = 5, a >= 6 is true, so B = 5 on the right side does not participate in the operation, so B is equal to the original 0.
Conditional operator
The symbol is this?: A question mark and a colon, an English character. The only three-mesh operator in the C language.
The general form of a conditional expression consisting of a conditional operator is:
Conditional expression? Expression 1: Expression 2
If the conditional expression is true, the value of the entire expression is the value of expression 1;
If the conditional expression is false, the value of the entire expression is the value of expression 2.
Find the maximum of three numbers, which is written using the conditional operator:
int a = 5;
int b = 6;
int c = 9;
int max = a > B? A:B;
max = max > c? Max:c;
It can also be written, simpler
//int max = a > B? (A > C. a:c): (b > C. b:c);
int a = 5;
int b = 6;
int c = 9;
int max = a > B? A:B;
max = max > c? Max:c;
It can also be written, simpler
//int max = a > B? (A > C. a:c): (b > C. b:c);