------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Trinocular operator
The trinocular operator (the conditional operator) is? And:, he is a three-mesh operator, which is the amount of three operands involved in the operation.
The general form of a conditional operator consisting of a conditional expression is:
Expression 1? Expression 2: Expression 3
Evaluation rules
If the value of expression 1 is true, the value of expression 2 is used as the value of the conditional expression
Otherwise, the value of expression 2 is used as the value of the entire conditional expression.
Conditional expressions are typically used in the center of an assignment statement.
For example, a conditional statement:
if (a>b) max = A;
else max = b;
You can use conditional expressions to write: max= (a>b)? A:B;
The semantics for executing the statement are: If A>b is true, assign a to Max, or assign B to max.
Program Examples:
#include <stdio.h>
int main (int argc, const char*argv[]) {
int a = 3; b = 4; result = 0;
result = a>b?10:100;
printf ("Result =%d\n", result);
return 0;
}
Precautions
1. The operator precedence of the conditional operator is lower than the relational and arithmetic operators.
2. Conditional operators? And: is a pair of operators that cannot be disassembled for use alone.
3. The binding direction of the conditional operator is from right to left.
Dark Horse Programmer---iOS learning log 9