Java branch statement classification
Branch statement are selectively performed or skipped particular statement according to certain conditions, divided into two categories:
if-else statement
switch statement
if-else statement syntax
IF (boolean expression) { statement or block of statements; } IF (boolean expression) { statement or block of statements; } the else IF (boolean expression) { statement or block of statements; } the else { statement or block of statements; }
if-else statement Application Example
Package com.uncleyong; public class TestIf { public static void main (String [] args) { // if age <= 8, print "no school" // if age <= 14, prints out the "primary" // if age <= 17, print "junior high school" // If age <= 20, print "high school student" // If age <= 24, print "students" // if not met, print "graduate" int Age = 5 ; IF (Age <=. 8 ) { System.out.println ( "no school" ); } the else IF (Age <= 14){ System.out.println ( "primary" ); } the else IF (Age <=. 17 ) { System.out.println ( "junior high school" ); } the else IF (Age <= 20 is ) { System.out.println ( " High School " ); } the else IF (Age <= 24 ) { System.out.println ( " Students " ); } the else { System.out.println ( " graduated " ); } } }
The switch statement syntax
Switch (expression) { Case constant 1: sentence 1; BREAK ; Case constant 2: Statement 2; BREAK ; ...... Case constant N: Statement N; BREAK ; [ default : statement; BREAK ;] }
The switch statement Application examples
Package com.uncleyong; Import java.util.Scanner; public class SwichScore { public static void main (String [] args) { / * programming: the keyboard is read from a student performance, is stored in the variable score according to the score value of the output corresponding grades: score> = 90 level: a 70 = <score <90 level: B 60 = <score <70 level: C score <60 level: D * / Scanner Scanner = new new Scanner (the System.in ); System.out.print ( "Please enter student achievement, Score =" ); int Score = scanner.nextInt(); switch(score/10){ case 10: case 9: System.out.println("A"); break; case 8: case 7: System.out.println("B"); break; case 6: System.out.println("C"); break; default: System.out.println("D"); } } }
The switch statement relevant rules
switch (expression) of the expression of the return value must be one of several types: int, byte, char, short , enum, string;
value in the case clause must be constant, and all case clause value should be different;
default clause is optional;
BREAK statement is used after executing programs out of a case that the switch statement branch block;
Statement, article reproduced from the full-stack test notes https://www.cnblogs.com/UncleYong/p/9729156.html Thanks for sharing the power of God.