Simple analysis of C # operation Fu Zhi ternary operator _c# Tutorial

Source: Internet
Author: User

C # operation Fu Zhi ternary operator "?:" What is it?

C # Operations Fu Zhi The ternary operator "?:" is sometimes called a conditional operator.

For conditional expression b?x:y, the condition B is evaluated first and then judged.

If the value of B is true, the value of x is computed and the result of the operation is the value of x, otherwise, y is computed with a value of Y.

A conditional expression never computes X, and also calculates Y. The conditional operator is associated to the right, that is, the grouping is computed from left to right.

C # operations Fu Zhi ternary operator "?:" Action instance:

The expression a?b:c?d:e will be executed in the form of A?b: (c?d:e).

?: The second and third operands control the type of the conditional expression. Set X and Y to be the type of the second and third operands, respectively, then:

If x and Y are the same type, then the type is the type of the conditional expression.

Otherwise, if there is an implicit conversion from X to Y, but there is no conversion of Y to X, then Y is the type of the conditional expression.

Otherwise, if there is an implicit conversion from Y to X, but there is no conversion of x to Y, then X is the type of the conditional expression.

Otherwise, no expression type is defined, and a compile-time error occurs

C # operation Fu Zhi ternary operator "?:" the basic content to introduce you here, hope to you understand and learn C # operation Fu Zhi ternary operator "?:" is helpful.

The ternary operator also becomes a conditional operator, and he appears to be more special because there are three operands, but he does belong to an operator.
Its form as
Boolean-exp?value0:value1
If the BOOLEAN-EXP expression evaluates to True, the VALUE0 is computed and the result is the resulting value of the operator. If the BOOLEAN-EXP expression evaluates to False, the value1 is computed, and likewise his result becomes 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, operators will produce a value.

Copy Code code as follows:

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, ternary operators are much more compact and if-else easier to understand.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.