One:
Int I = 0;
Switch (I)
{
Case 0:
Console. writeline ("0 ");
Case 1:
Console. writeline ("1 ");
Break;
}
// Here, you want to execute
// Console. writeline ("0"), and then "fall through" to the lower layer to execute case 1.
// However, "fall through" is not allowed in C. Unless none of the "case 0" statements exist.
However, you can use the GOTO statement to implement "fall through"
int i = 0;switch (i){ case 0: Console.WriteLine("0"); goto case 1; case 1: Console.WriteLine("1"); break;}
In C #, every case XXX is a tag, so you can use the GOTO statement to redirect.
Two:
Here, the default statement does not do anything, but does not add break;
The prompt is as follows:
C # Strictly control that the branches of each switch are not allowed to run through ("fall through"). For example, sometimes you may look like the following:
This statement is completely legal.
Three:
Sometimes you define variables in case, but the variables may have the same name, for example:
Two identical variables, Y, are defined in case 0 and Case 1. The Compiler prompts the following error:
To solve this problem, add "{}" to convert the case statement into a BLOCK statement.
Of course, there is also an unknown solution, as shown below:
Four:
Suppose you have a method m and the code is as follows:
int M(bool b){ switch (b) { case true: return 1; case false: return 0; }}
It is obvious that B has only two values, one being true and the other being false, but the compiler fails. The prompt is:
The compiler considers that every switch structure can be executed and does not execute it. However, method M must have an int return value, so the compiler prompts an error.
The solution to this problem is also simple:
int M(bool b){ switch (b) { case true: return 1; default: return 0; }}
Or:
int M(bool b){ return b ? 1 : 0;}
Original article: four switch oddities
One:
Int I = 0;
Switch (I)
{
Case 0:
Console. writeline ("0 ");
Case 1:
Console. writeline ("1 ");
Break;
}
// Here, you want to execute
// Console. writeline ("0"), and then "fall through" to the lower layer to execute case 1.
// However, "fall through" is not allowed in C. Unless none of the "case 0" statements exist.
However, you can use the GOTO statement to implement "fall through"
int i = 0;switch (i){ case 0: Console.WriteLine("0"); goto case 1; case 1: Console.WriteLine("1"); break;}
In C #, every case XXX is a tag, so you can use the GOTO statement to redirect.
Two:
Here, the default statement does not do anything, but does not add break;
The prompt is as follows:
C # Strictly control that the branches of each switch are not allowed to run through ("fall through"). For example, sometimes you may look like the following:
This statement is completely legal.
Three:
Sometimes you define variables in case, but the variables may have the same name, for example:
Two identical variables, Y, are defined in case 0 and Case 1. The Compiler prompts the following error:
To solve this problem, add "{}" to convert the case statement into a BLOCK statement.
Of course, there is also an unknown solution, as shown below:
Four:
Suppose you have a method m and the code is as follows:
int M(bool b){ switch (b) { case true: return 1; case false: return 0; }}
It is obvious that B has only two values, one being true and the other being false, but the compiler fails. The prompt is:
The compiler considers that every switch structure can be executed and does not execute it. However, method M must have an int return value, so the compiler prompts an error.
The solution to this problem is also simple:
int M(bool b){ switch (b) { case true: return 1; default: return 0; }}
Or:
int M(bool b){ return b ? 1 : 0;}
Original article: four switch oddities