The Java Basics switch

Source: Internet
Author: User

The format of the switch statement:

 

Switch (integer or character variable)

{

The case variable may have a value of 1:

Branch one;

Break

The case variable may have a value of 2:

Branch two;

Break

The case variable may have a value of 3:

Branch three;

Break

...

Default:

The last branch;

 

In switch syntax, we have to learn 4 keywords: switch, case, break, default.

 

In the switch (variable) line, the variable can only be integer or character type. The program reads out the value of the variable, and then finds in each "case" which value is equal to the variable, and if so, if the condition is true, the program executes the corresponding branch until the break or switch statement ends.

 

 

Understanding the meaning of Switch,case,break,default is also helpful in understanding everything in front of you: switch, case, interrupt, default (value). Then in a word to set up the argument is: according to the different values of the switch, the implementation of different situations, until the interruption; if all cases do not meet the switching value, then the default branch is executed.

 

Finally, here are 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 integer and character type. They contain Int,char. Of course, no character type or different length integer (unsigned int,short,unsigned char) can be. Also, an enum type (enum) is internally implemented by an integral type or character type. So it can be. The number of real (floating-point) can not be, such as:

float a = 0.123;

Switch (a)//error! A is not an integer or character-type variable.

{

....

}

 

Second, the case can be a direct constant value, such as the example of 1, 2, 3, 4, can also be a constant calculation, such as the one, but not a variable or an expression with a variable, such as a * 2. Of course, it can not be a real number, such as 4.1, or 2.0/2.

Switch (Formway)

{

Case 2-1://correct

...

Case A-2://Error

...

Case 2.0://Error

...

}

Also, after the case and the constant value, you need a colon, be careful not to neglect it.

 

Third, the role of break.

Break allows the program to complete a switch by jumping out of the entire switch statement (that is, after jumping to a pair of {} on a switch) after the selected branch is executed. Without this break, the program will proceed to the next branch until it encounters a trailing break or switch is complete.

For example, suppose the program now enters the branch in case 1: But the branch of Case 1 does not add a break:

 

Case 1:

System.out.println ("You are coming to this site through a search engine.") ");

Case 2:

System.out.println ("You are introduced to this website through a friend.") ");

 

Then, the program outputs "you are coming to this site through a search engine." "After that, you will continue to output in case 2," you are introduced to this website through a friend. " 。

Please change the code snippet in the previous instance to the following (the red part, all the break will be invalid by adding//making it. ):

...

Case 1:

System.out.println ("You are coming to this site through a search engine.") " );

Break

Case 2:

System.out.println ("You are introduced to this website through a friend.") ");

Break

Case 3:

System.out.println ("You came to this website through a newspaper magazine.") ");

Break

Case 4:

System.out.println ("You are coming to this website by other means.") ");

Break

Default:

System.out.println ("the wrong choice! Please enter 1~4 number to make a selection. ");

...

 

How will the results be after the run? Please try them out and answer me in the homework.

 

The default is optional, before we have already said its usefulness, and if there is no default, the program cannot 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 the instance, and then try to run it, and enter 5 on the selection.

...

Default:

cout << "Wrong choice! Please enter 1~4 number to make a selection. "<< Endl;

...

 

If necessary, you can use {} in each case to explicitly produce a separate compound statement.

We're talking about the If ... Statements and other Process Control statements, you use {} to produce compound statements:

if (condition)

{

Branch one;

}

Unless the statement in the branch is just one sentence, the curly braces {} may not be required here. However, in the various case statements of switch, we do not mark the syntax format to use {}, see:

Switch (integer or character variable)

{

The case variable may have a value of 1:

Branch one;

Break

The case variable may have a value of 2:

....

}

 

The general textbook only says that the case branch can not use {}, but here I would like to remind you that not all cases can be a branch without {}, for example, you want to define a variable in a particular instance:

Switch (Formway)

{

Case 1:

int a=2; Error. Because of the ambiguous scope of the case, the compiler cannot define a variable here.

...

Case 2:

...

}

 

In this case, add {} To resolve the issue.

Switch (Formway)

{

Case 1:

int a=2; Correctly, variable A is explicitly scoped to the current {} range.

...

Case 2:

...

}

The Java Basics 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.