Asp tutorial. net ternary operators
The ternary operator is also a conditional operator. It seems special because it has three operands, but it does belong to one of the operators.
The format is
Boolean-exp? Value0: value1
If the boolean-exp expression returns true, value0 is calculated, and the calculation result is the final value produced by the operator. If the boolean-exp expression returns false, value1 is calculated. Similarly, the result is the final value of the operator.
Of course, it can also be replaced by if-else, but the ternary operator and if-else are completely different, and the operator will generate a value.
Public class ternaryifelse {
Static int ternary (int I ){
Return I <10? I * 100: I * 10;
}
Static int standardifelse (int I ){
If (I <10)
Return I * 100;
Else
Return I * 10;
}
Public static void main (string [] args ){
System. out. println (ternary (9 ));
System. out. println (ternary (10 ));
System. out. println standardifelse (9 ));
System. out. println standardifelse (10 ));
}
}
Output
900
100
900
100
In contrast, the ternary operators are much more compact, and if-else is easier to understand.