Multiple choice structure switch statement

  • 2020-04-01 01:22:56
  • OfStack

Multiple select structure switch statement
The multipath branch selection process in Java is specifically provided with a switch statement that selects one of several operations to run based on the value of an expression. His grammatical form is as follows:
 
switch( expression ){ 
case  Expression constant 1 Statements: 1 ;  
break ;  
case  Expression constant 2 Statements: 2 ;  
break ;  
...... 
case  Expression constant n Statements: n ;  
break ;  
[default Statements: n+1 ; ] 
} 

Where a case expression constant becomes a label that represents the entry to a case branch. The switch statement first evaluates the "expression" in the switch parenthesis at run time, which must be of integer or character type, and the values of the subsequent case expression constants should be of the same type as the "expression" in the switch parenthesis. A case statement represents a specify action and then moves 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 exits.
Finally, a few important points to note about switch.

First, in the switch (integer or character variable), the type of the variable as marked in the text, can only be integer and character types. They contain int char. Of course,unsigned types or different length integers (unsigned int,short,unsigned char) can be used. In addition, enumerated types (enum) are also implemented internally by integer or character types. So it works. Not for real (floating point) Numbers, such as:
 
float a = 0.123; 
switch(a) //Error! A is not an integer or character type variable.
{ 
.... 
} 
 

Second, after case, it can be a direct constant value, such as 1, 2, 3, 4 in the example, or it can be a constant calculation formula, such as 2+2, etc., but it cannot be a variable or an expression with variables, such as a * 2, etc. Of course, it cannot be a real type, such as 4.1, or 2.0/2, etc.
 
switch(formWay) 
{ 
case 2-1 : //correct
... 
case a-2 : //error
... 
case 2.0 : //error
... 
} 

Also, after case and constant values, a colon is required, so be careful not to neglect it.

Third, the effect of break. Break allows the program to complete the switch after executing the selected branch by jumping out of the entire switch statement (that is, after a pair of {} s connected to the switch). If there is no break, the program continues on to the next branch until it encounters a later break or switch finish.
For example, suppose the program now enters a branch in case 1:, but the branch in case 1 is not broken:
 
case 1 : 
System.out.println(" You came to this site through a search engine. "); 
case 2 : 
System.out.println(" You are introduced to this website by a friend. "); 
 

So, the program says, "you came to this site through a search engine." After that, the output of case 2 continues, "you were introduced to this site by a friend." .

Fourth, default is selectable, previously we have said its use, and if there is no default, the program can not find a matching case branch, will not do anything in the switch statement range, directly complete the switch. You can also comment out the default code in the instance, try it out, and enter a custom one when you select it.

Fifth, if necessary, {} can be used in each case to explicitly produce separate compound statements. We were talking about if... Statements and other flow control statements use {} to generate compound statements:
 
if ( conditions ) 
{ 
 A branch ; 
} 

Unless there is exactly one statement in the branch, curly braces {} are not required. However, in the case statements of switch, we have not marked the syntax format to use {}, see:
 
switch (  Integer or character variables  ) 
{ 
 case  Possible value of variable 1  :  
   A branch ; 
 break; 
case  Possible value of variable 2  :  
.... 
} 
 

The textbook only says that case branches can be used without {}, but I want to remind you that case branches can not be used without {} in every case, such as if you want to define a variable in a case:
 
switch (formWay) 
{ 
case 1 : 
int a=2; //Error. Because of the unclear scope of the case, the compiler cannot define a variable here.
... 
case 2 : 
... 
} 
 

In this case, adding {} solves the problem.
 
switch (formWay) 
{ 
case 1 : 
 {  
int a=2; //True, variable a is explicitly limited to the current {} range.
... 
 }  
case 2 : 
... 
} 

Finally, take a look at the example program:
 
public class TestSwitch //Character-based type
{ 
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(" good ."); 
break; 
case 'B': 
System.out.println(" good ."); 
break; 
case 'C': 
System.out.println(" In the "); 
break; 
case 'D': 
System.out.println(" Pass the "); 
break; 
case 'F': 
System.out.println(" Don't pass the "); 
break; 
default: 
System.out.println(" Grade input error "); 
} 
} 
} 

Related articles: