Java learning notes (entry) _ multiple choice structure switch statements

Source: Internet
Author: User
Tags case statement

Multiple choice structure switch statement
In java, a switch statement is provided for the multi-path Branch selection process. The switch statement selects one of the multiple operations to run based on the value of an expression. The syntax format is as follows: Copy codeThe Code is as follows: switch (expression ){
Case expression constant 1: Statement 1;
Break;
Case expression constant 2: Statement 2;
Break;
......
Case expression constant n: Statement n;
Break;
[Default: Statement n + 1;]
}

A case expression constant becomes a label, which represents the entry of a case Branch. When running the switch statement, the value of the "expression" in the switch parentheses must be calculated. The value must be of the integer or balanced type, at the same time, the type of the values of the subsequent case expression constants should be consistent with the value type of the "expression" in the switch parentheses. A case statement represents a specific operation and then redirects to the structure exit. The default clause is optional. When the expression value does not match the constant value of the case expression, run the default clause to switch to the structure exit.
Finally, let's take a few important notes about the switch.
 
1. In switch (an integer or character type variable), the type of the variable, as marked in the text, can only be an integer or character type. They include int and char. Of course, none of the character types or different length INTEGER (unsigned int, short, unsigned char) can all be. In addition, the enumeration type (enum) is also implemented by the integer or character type. So you can. The number of real instances (floating point instances) cannot be used, for example:Copy codeCode: float a = 0.123;
Switch (a) // error! A is not an integer or character type variable.
{
....
}
 

2. After case, it can be a direct constant value, for example, 1, 2, 3, and 4. It can also be a constant calculation formula, such as 2 + 2, but it cannot be a variable or an expression with a variable, such as a * 2. Of course, it cannot be a real number, such as 4.1, or 2.0/2.Copy codeThe Code is as follows: switch (formWay)
{
Case 2-1: // correct
...
Case A-2: // The Error
...
Case 2.0: // an error occurs.
...
}

In addition, a colon is required after case and constant value. Be careful not to neglect it.
 
Third, the role of break. Break enables the program to jump out of the entire switch statement (that is, after one pair of {} after the switch is connected) after running the selected branch to complete the switch. Without this break, the program will continue to the next branch until the next break or switch is met.
For example, assume that the program now enters the branch in case 1: But the branch in case 1 does not add break:
 Copy codeThe Code is as follows: case 1:
System. out. println ("You came to this website through the search engine. ");
Case 2:
System. out. println ("You came to this website through a friend's introduction. ");
 

Then, the program outputs "you came to the website through the search engine. "Later, we will continue to output" in case 2, "you came to this website through a friend introduction. ".
 
Fourth, default is optional. We have already mentioned its usefulness. If there is no default, the program will not do anything within the range of the switch statement after the matching case branch cannot be found, directly complete the switch. You can also comment out the default code in the instance, run the test, and enter the custom code during the selection.
 
Fifth, if necessary, you can use {} in each case to explicitly generate an independent compound statement. When we talk about the if... statement and other process control statements, both use {} to generate compound statements:Copy codeThe Code is as follows: if (condition)
{
Branch 1;
}

Unless there is only one statement in the branch, brackets {} are not required here {}. However, in the case statements of the switch, the syntax format is not marked as {}. Please refer:Copy codeThe Code is as follows: switch (integer or complex variable)
{
Case variable value 1:
Branch 1;
Break;
Case variable value 2:
....
}
 

In general, the case Branch does not use {}, but I would like to remind you that {} can not be added to the case Branch in any case {}, for example, you want to define a variable in a case:Copy codeThe Code is as follows: switch (formWay)
{
Case 1:
Int a = 2; // error. The compiler cannot define a variable here because the range of case is not clear.
...
Case 2:
...
}
 

In this case, adding {} can solve the problem.Copy codeThe Code is as follows: switch (formWay)
{
Case 1:
{
Int a = 2; // correct. Variable a is explicitly limited to the current {} range.
...
}
Case 2:
...
}

Finally, let's take a look at the example program:Copy codeThe Code is as follows: public class TestSwitch // role-based
{
Public static void main (String [] args)
{
// Declare the variable score and assign it to 'C'
Char score = 'C ';
// Execute the swicth branch statement
Switch (score)
{
Case 'A ':
System. out. println ("excellent .");
Break;
Case 'B ':
System. out. println ("good .");
Break;
Case 'C ':
System. out. println ("medium ");
Break;
Case 'D ':
System. out. println ("pass ");
Break;
Case 'F ':
System. out. println ("fail ");
Break;
Default:
System. out. println ("Incorrect Score Input ");
}
}
}

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.