Java-preliminary Understanding-Chapter III-statement-switch

Source: Internet
Author: User

I. Overview

The judging structure of the IF statement can be regarded as a judgment question, and the choice structure of switch is regarded as the choice problem. Judgement two results choose one, the choice of the question is more.

A preliminary understanding of switch: the difference between switch and if, in addition to one is the choice, one is to judge, there is a significant difference in the control of the operation, switch is a number of options are tested, different from if there is a decision to exit the operation.

Two. Knowledge of switch

Switch as a more complex statement, which involves more keywords, there are so four, switch,case,break,default.

→ Note: whether to judge the structure if statement, or choose the structure switch statement, there will be a premise, that is, the data obtained, and then the obtained data to be judged. Switch statement, the action of the corresponding option is to see which option the variable belongs to.

Note: swithc (conditional expression), the conditional expression in parentheses should refer to the equation, which is different from the result of the Boolean type in the If statement, which appears to be a numerical result. The following case is followed by a numerical result, when the value after a certain case, the operation of the corresponding execution statement, and finally exit. If one does not correspond, the final default is executed. Also, when a switch statement is executed, the entire block of code is loaded into memory.

The switch statement is a selection statement that allows you to choose from several types of data: Short,byte,int,char.

Note: See the above example, you can know, 1. In the selection structure, the execution of the statement is not a simple output, but can be performed, which is the execution of the statement, as long as the statement can be. 2. The character type appearing here is written in the expression to write the variable name directly, and the understanding of the conditional expression is not enough . 3. The number of case is also uncertain.

In a conditional expression, there are four types of values.

The alternates in the switch statement are unordered, can be swapped in order, and the default is placed in the first position, but what happens when the underlying is in operation ? Placement is not sequential, but execution is sequential, starting with the first case, and then executing the default when each case is finished.

One of the two ways to end a switch statement

One way to end is to do it through break, and the other is to omit break when the last case is close to it, which is not really understood.

Note: Let's take a look at the second way to end a switch. In the second end, when there is a break, there is no break keyword, and the switch statement is terminated by something else. In this example, the default is the first, but the execution is still starting from Case 4, because each case does not correspond, so that the inside of the break will not execute (if any), and then jump to execute the default, you can execute the statement, but there is no break, cannot exit from here, Can only continue to execute, each case is executed, the output of each statement (in this case, it is not much like a switch statement.) ), the switch statement exits when a break or} is encountered.

Java-preliminary Understanding-Chapter III-statement-switch

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.