Detailed explanation of ternary operator
For some selection branch structures, simple conditional operators can be used instead . For example:
if(a<b) min=a; else min=b;
You can use the following conditional operator to process
min=(a<b)?a:b;
where "(a
The conditional operator consists of two symbols "?" and ":". It requires three operating objects, so it is also called the ternary operator. It is the only ternary operator in the C language. operator.
Its general form is:
表达式1?表达式2:表达式3;
The following are some instructions about conditional operators:
(1) Expression 1 is a relational expression or logical expression, used to describe conditions, expression 2 and expression 3 can be constants, variables or expressions. For example:
(x==y)?'Y':'N' (d=b*b-4*a*c)>=0?sqrt(d):sqrt(-d) ch=(ch>='A'&&ch<='Z')?(ch+32):ch
The above are all legal conditional expressions.
Related recommendations: "FAQ"
(2) Execution order: Solve expression 1 first. If the value is non-0, it means the condition is true. , then find expression 2. At this time, the value of expression 2 is used as the value of the entire conditional expression;
If the value of expression 1 is 0, it means that the condition is false, then solve the expression 3. The value of expression 3 is the value of the entire conditional expression. For example:
(a>=0)?a:-a The execution result is the absolute value of a
(3) In the program, by changing the conditional expression The value is assigned directly to a variable. For example:
min=(a
(4) The precedence level of conditional expressions is only higher than the assignment operator, but lower than all previously encountered operators.
Therefore, min=(a
(5) Conditional operator The binding direction is "right to left".
(6) Conditional expressions are allowed to be nested, that is, expression 2 and expression 3 in the conditional expression are allowed to be another conditional expression. For example:
x>0?1:x<0?-1:0
In the above conditional expression, the 3rd part of the expression is another conditional expression. According to the associativity of the conditional expression, the above conditional expression is equivalent to:
x>0?1:(x<0?-1:0)
Its function is to determine the sign of x. When x is a positive number, the value of the conditional expression is 1; when x is a negative number, the value of the conditional expression is -1; when x is 0, the value of the conditional expression is 0.
(7) The conditional expression can replace the if statement only when the statement embedded in the if statement is an assignment statement (and both branches assign values to the same variable). For example:
if(a%2==0) printf("even/n"); else printf("odd/n");
cannot be written as:
(a%2==0)?printf("even/n"):printf("odd/n");
but can be replaced by the following statement:
printf("%s/n",(a%2==0?"even":"odd");
The function of this statement is: if a is an even number, output even; If a is an odd number, output odd.
(8) The types of expression 1, expression 2 and expression 3 can be different. At this time, the value type of the conditional expression is the higher type among them. For example:
main() { char c1, ch; ch = getchar(); c1 = ch <= 'Z' && ch >= 'A' ? ' @ ' : ch ; putchar(c1); }
The function of this program is to input any character from the keyboard and determine whether they are capital letters. If so, output an @; otherwise, output do as it is. In the above example, line 6 of the program is the key statement. , this statement realizes the judgment, selection and replacement of input characters.
The execution process is: first, the conditional operator on the right side of the assignment number in this statement judges and selects the input characters. If ch>='A'&&ch<='Z' is true, explain ch is an uppercase English letter, so select @ at this time; otherwise, still select the original character ch, and then assign the selected result to the original variable ch.
The above is the detailed content of What is the ternary operator. For more information, please follow other related articles on the PHP Chinese website!