We know that in C, we often use judgment statements such as if (expression) {} And while (expression) {}. When the expression is true, we execute the function body in, but how can I determine whether the expression is true or false when the expression is a value expression? Or how can I use a comma expression?
Remember one principle: the value represented by the C language value assignment expression is the value that is finally assigned to the assigned variable.
The following test code proves the above points:
View code
1 #include <stdio.h> 2 3 int main(int argc, const char *argv[]) 4 { 5 int i; 6 printf("the value is %d\n", i = -1); 7 printf("the value is %d\n", i =123); 8 printf("the value is %d\n", i = 0); 9 return 0;10 }
The result of the following code is easy to understand!
1 # include <stdio. h> 2 3 int main (INT argc, const char * argv []) 4 {5 Int I; 6 // when the value is 0, the value of the value expression indicates that the logical value is false, therefore, the following statement does not execute 7 if (I = 0) {8 printf ("I = 0 \ n "); 9} 10 // when the value is not 0, the value assignment expression indicates that the logical value is true, so the following statement will execute 11 if (I = 1) {12 printf ("I = 1 \ n"); 13} 14 // when the value is not 0, the value assignment expression indicates that the logical value is true, therefore, the following statement will execute 15 if (I =-1) {16 printf ("I =-1 \ n "); 17} 18 // The logical value represented by a comma expression is the logical value of the last expression, so the following statement does not execute 19 if (I = 1, I = 0) {20 printf ("I = 1, I = 0 \ n"); 21} 22 // The logical value represented by a comma expression is the logical value of the last expression, therefore, the following statement runs 23 if (I = 0, I = 1) {24 printf ("I = 0, I = 1 \ n "); 25} 26 27 28 29 return 0; 30}