Switch () Parameters

Source: Internet
Author: User

The author is unknown because it is transferred from the reprint. Here, I would like to thank @ author.

1. Switch Parameter type:
The expression behind the switch cannot be double, float, long, String, Boolean. It can be connected to int, short, byte, Char!
2. variable definition in switch:
Problem:
Switch ()
{
Case 1:
Cstring STR = "ABCDE"; // This statement is incorrectly compiled. Why? => You can only add {} to the switch to define variables.
Break;
Case 2:
Break;
....
}

Reply 1:
Switch ()
{
Case 1:
{
Cstring STR = "ABCDE"; // OK
}
Break;
Case 2:
Break;
....
}
It's okay to add curly braces. This is because if A is 2, the variable initialization statement will not be executed,
Reply 2:
A switch is a separate program segment. Case (without {}) is not a separate program segment. Let's look at this example:
Int J = 1;
Switch (j ){
Case 1: int B; break;
Case 2: B = 1; break;
}
Can be compiled. Msdn has the following instructions:
Compiler error c2360
Initialization of 'identifier' is skipped by 'case' label
The specified identifier initialization can be skipped in a switch statement.
It is illegal to jump past a declaration with an initializer unless the declaration is enclosed in a block.

The scope of the initialized variable lasts until the end of the switch statement unless it is declared in an enclosed block within the switch statement.

The following is an example of this error:
Void func (void)
{
Int X;
Switch (X)
{
Case 0:
Int I = 1; // error, skipped by Case 1
{Int J = 1;} // OK, initialized in enclosing Block
Case 1:
Int K = 1; // OK, initialization not skipped
}
}
Reply 3:
For switch (k)
{
Case 1:
Int I;
Case 2:
I = 0;
}
If you follow the whjpn (usually flat) idea, the problem may occur:
If k = 1, nothing
If K = 2, who defines I? How can this problem be solved?
Therefore, the solution to the above problems is:
1. Declare variables outside the switch so that case 2 will not be wrong.
2. Declare the local variable in the switch. The flag of the local variable is {}
That is, Case 1:
{Int I ;}
But case 2 won't be able to use I anymore.
In addition
Int K = 2;
Switch (k)
{
Case 0:
Int I; // OK
Int J = 1; // error cannot be defined,
Break;
Case 1:
{
Int I = 1; // If {} is added, it can be defined, but it is a local variable.
}
Break;
Case 2:
I = 2; // OK
Diag_printf ("kkkkkkkkkkkkkkkkk: I = % d \ n", I );
Default :;
}
During compilation, I of case 0 is treated as a variable in the switch, so even if k = 2, I is still assigned a value of 2.
That is, the switch variable cannot be declared and assigned a value in case.

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.