Relationship expressions in Whlie loops
While loops often rely on test expressions for comparison, which are called relational expressions.
For example, while (index < 5) where (index < 5).
This expression is an entry condition because the condition must be satisfied, that is, true, to enter the body of the loop. If the condition is false, the loop cannot be entered.
What is true?
In C, expressions must have a value, and relational expressions are no exception.
#include <stdio.h>
int main(void)
{
int true_v, false_v;
true_v = (10 > 2); // The relationship is true
false_v = (10= =2); // The relationship is false
printf("true= %d; false= %d",true_v,false_v);
return 0;
}
Copy the code
The program assigns the values of two relational expressions to two variables, namely, true_v for true and FALSE_v for false_v. After running the program, the output is as follows:
true= 1; false= 0
Copy the code
You can see that for C, the value is 1 if the expression is true and 0 if the expression is false.
Some C programs use the following loop structure, which keeps going because 1 is true.
while(1) {... }Copy the code
Other true value
Since 1 or 0 can be used as test expressions for a while statement, can other numbers be used?
#include <stdio.h>
int main(void)
{
int n = 3;
while (n){
printf("%2d is true\n", n--);
}
printf("%2d is false\n", n);
n = - 3;
while (n){
printf("%2d is true\n", n++);
}
printf("%2d is false\n", n);
return 0;
}
Copy the code
The output of the program is as follows:
3 is true
2 is true
1 is true
0 is false
-3 is true
-2 is true
-1 is true
0-is alse
Copy the code
When the first loop is executed, n is 3, 2, 1, and when n equals 0, the first loop ends. Similarly, the second loop is executed with n being -3, -2, and -1, and the second loop ends when n is equal to 0.
The conclusion is that in general, all non-zero values are treated as true, and only 0 is treated as false.
Logical operator
The and, or, and not three operators.
symbol | meaning |
---|---|
! | Logic is not |
&& | Logic and |
|| | Logic or |
The logical operator produces a result of 0 or 1.
Here is the case where the expression is 1. Otherwise, these operators yield 0.
Logical no: If the value of the expression is 0, then the value of the expression is 1.
int a = 0;
if(! a){//a = 0; The value of a is 1, so if condition 1 prints Hello
printf("Hello World!");
}
Copy the code
Logical and: If both the values of expression 1 and expression 2 are non-zero, then expression 1 && expression 2 results in 1.
int a = 3, b = 2;
if (a==3 && b==2) {// the expression a==3 and b==2 are both non-zero, so the if condition is 1 and the output is Hello
printf("Hello World!");
}
Copy the code
Logic or: if the expression 1 and 2 in the value of any one (or both) a non-zero value, then the expression of 1 | | expression results of 2 to 1.
int z = 0, y = 2;
if (z==3 && y==2) {// if z==3 is zero, y==2 is nonzero enough
printf("Hello World!");
}
Copy the code
Case where logic is not in if
if(a) is equal toif(a ! =0)
if(! A) is equivalent toif (a == 0)
Copy the code
&& and | | short circuit characteristics
“Short circuit” of operator &&
int a = 0, b = 0;
// if a==1 is 0, then b==0 is not evaluated.
if ( a==1 && b==0) {printf("Hello");
}
Copy the code
The operator | | “short circuit”
int a = 0, b = 0;
// If a==0 is 1, then b==0 will not be evaluated.
if ( a==0 || b==0) {printf("Hello");
}
Copy the code