Java Learning notes (Getting started) _ Multi-selection structure switch statement _java

Source: Internet
Author: User
Tags case statement constant
Multi-Select structure switch statement
In Java, a switch statement is specifically provided for the multipath branching selection process, and the switch statement chooses one of the multiple operations to run based on the value of one expression. His grammatical form is as follows:
Copy Code code 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;]
}

Where a case expression constant becomes a label, representing the entry of a case branch. The switch statement computes the value of an expression in switch parentheses first, which must be integral or character, and the type of the value of the subsequent case expression constants should be the same as the value type of the expression in switch parentheses. A case statement represents a set operation and then shifts to the structure exit. The default clause is optional, and when the value of the expression does not match the value of the case expression constant, the default clause is run and the structure exit is turned.
Finally, a few important points to note about switch.
 
First, switch (integer or character variable), the type of the variable, as indicated in the text, can only be integral and character types. They contain Int,char. Of course, no type or different length integral type (unsigned int,short,unsigned char) can be. In addition, an enumeration type (enum) is also implemented internally by an integer or character type. So it can be. The number of solid (floating-point type) is not good, such as:
Copy Code code as follows:

float a = 0.123;
Switch (a)//error! A is not an integer or character-type variable.
{
....
}
 

Second, the case can be directly after the constant value, such as the example of 1, 2, 3, 4, can also be a constant calculation, such as 2+2, but can not 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 Code code as follows:

Switch (Formway)
{
Case 2-1://correct
...
Case A-2://Error
...
Case 2.0://Error
...
}

In addition, after case and constant value, a colon is required, please be careful not to neglect.
 
Third, the role of the break. The break causes the program to complete the switch after the selected branch has been executed, jumping out of the entire switch statement (that is, after a pair of {} switch connectors). Without this break, the program continues to advance to the next branch until it encounters a later break or switch complete.
For example, suppose that the program now goes into the branch in case 1: But the branch of Case 1 does not break this time:
 
Copy Code code as follows:

Case 1:
System.out.println ("You are coming to this site through search engines.") ");
Case 2:
System.out.println ("You are introduced to this website through a friend.") ");
 

So, the program in the output "you are through the search engine came to this site." "After that, you will continue to export the case 2," you are introduced to this site through a friend. " 。
 
Four, the default is optional, we have already said its usefulness, and if there is no default, the program can not find a matching case branch, will not do anything within the scope of the switch statement, directly complete the switch. You can also comment out the default code in an instance, and then try to run it, and then enter the custom at the time of the selection.
 
When necessary, you can use {} in each case to explicitly produce a separate compound statement. Before we talk about if ... Statement and other Process Control statements, you use {} to produce compound statements:
Copy Code code as follows:

if (condition)
{
Branch one;
}

Unless the statement in the branch is just one sentence, you may not need curly braces {}. But in the various case statements of the switch, we do not mark the syntax format to use {}, please see:
Copy Code code as follows:

Switch (integer or character type variable)
{
Case variable may be worth 1:
Branch one;
Break
Case variable may be worth 2:
....
}
 

The general textbook simply says the case branch can not use {}, but here I would like to remind you that it is not possible to have a case branch without {}, for example, if you want to define a variable in one of the cases:
Copy Code code as follows:

Switch (Formway)
{
Case 1:
int a=2; Error. The compiler cannot define a variable here because of the ambiguous scope of the case.
...
Case 2:
...
}
 

In this case, adding {} can solve the problem.
Copy Code code as follows:

Switch (Formway)
{
Case 1:

int a=2; Correct, variable A is explicitly qualified within the current {} scope.
...

Case 2:
...
}

Finally, take a look at the example program:
Copy Code code as follows:

public class Testswitch//Character-based
{
public static void Main (string[] args)
{
Declare a variable score and assign it a value of ' C '
Char score = ' C ';
Execute SWICTH Branch Statement
Switch (score)
{
Case ' A ':
System.out.println ("excellent.");
Break
Case ' B ':
System.out.println ("good.");
Break
Case ' C ':
System.out.println ("Zhong");
Break
Case ' D ':
SYSTEM.OUT.PRINTLN ("Pass");
Break
Case ' F ':
SYSTEM.OUT.PRINTLN ("fail");
Break
Default
SYSTEM.OUT.PRINTLN ("Score input error");
}
}
}
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.