Notes C # Basics Primer (20) Conditional operators for--c#

Source: Internet
Author: User

C # provides a conditional operator that can replace a simple if...else structure. The syntax for this conditional operator is as follows:

Conditional expression? Branch 1: Branch 2

?: is the conditional operator, you can see that it has 3 operands, so it is called a ternary operator. Its arithmetic logic is: when the conditional expression is true, branch 1 is executed, and when the conditional expression is false, branch 2 is executed.

In the following example, depending on the age, the output is "adult" or "minor":

Static void Main (string[] args) {    int;   Age    string backup;   Note    backup = Age >=?" Adult ":" minors ";    Console.WriteLine (Backup);}

Because of age=17, the value of the conditional expression Age>=18 is false, which returns "minors".

The above excerpt from the Web Course "C # development easy to get started"

Notes C # Basics Primer (20) Conditional operators for--c#

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.