The CASE expression returns a value based on the conditional logic.

Source: Internet
Author: User
Tags nested switch

The CASE expression returns a value based on the conditional logic.

In the development process, we often need to return a value for a column based on the conditional logic. In this CASE, we need to use the CASE expression.

For example, the following query of the Products table uses the CASE expression in the SELECT statement to generate information that describes the value of the categoryid column.

The SQL code is as follows:

-- Set the database context USE TSQLFundamentals2008; GOSELECT productid, productname, categoryid, CASE categoryidWHEN 1 THEN 'beverages 'WHEN 2 then' Condiments 'when 3 then' Dairy products' WHEN 4 then' Confections 'when 5 then' Grains/Cereals 'when 6 then' Meat/ poultry 'when 7 then' Produce 'when 8 then' Seafood 'else' Unkown Category 'end AS categorynameFROM Production. products;

Query results:


The expressions in the if statement are not limited to logical expressions, and can be any numeric type.

It can be any number, but in fact, the compilation system will forcibly convert this expression to a logical value. Generally, false is 0; 1 or any non-zero value is true, depending on the language.

Question
1. The expressions in the if statement are not limited to logical expressions, and can be any numeric type. [Y]
2. The switch statement can be completely replaced by the if statement. [Y]
3. The case expression of the switch statement must be a constant expression. [Y]
4. if statements, switch statements can be nested, and there is no limit on the number of layers of nesting. [Y]
5. the conditional expression can replace the if statement or replace the conditional expression with the if statement. [N]
6. The order of case and default of the switch statement does not affect the execution result. [N]
7. Multiple cases can execute the same program segment. [Y]
8. The inner break statement can terminate the nested switch so that the switch on the outermost layer ends. [N]
9. The case Branch of the switch statement can use the {} compound statement and multiple statement sequences. [Y]
10. The expression of the switch statement must be of the same type as the case expression. [Y]
11. In multiple switch branches, the break statement can immediately jump out of the switch statement body. [Y]
12. if (a = <B) x ++; is a valid C statement. [N]
13. if must be paired with else. [N]

Error: The case value "1" is in use

'A' | 'A' and 'B' | 'B' and 'C' | the 'C' expressions are all logical expressions, they all have 1 value, so there will be such a prompt. If you want to implement such a function, you can
Case 'A ':
Case 'A ':
Cout <"score: 90-100. ";
Break;
Case 'B ':
Case | 'B ':
Cout <"score: 80-90. ";
Break;
Case 'C ':
Case 'C ':
Cout <"score: 70-60. ";
Break;
Default:
Cout <"failed! ";

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.