3.1 Java branch structure

JAVA offers two of the most common branch control structure: if and switch statements

A, if conditional statement

There are three forms of the if statement.

The first form:
 IF (expression The Logic) 
{ 
    Statement ????? 
} 
second form 
IF (expression The Logic) 
{ 
    Statement ????? 
} 
the else (expression The Logic) 
{ 
    Statement ????? 
} 
third form 
IF (Logic expression The ) 
{ 
    statement ????? 
} 
the else  IF (expression The Logic) 
{ 
    statement ????? 
} 
????? // omitted plurality else if statements 
the else (expression The Logic) 
{ 
    statement ????? 
}

example:

 1 import java.util.*;
 2 class  ifTest
 3 {
 4     public static void main(String[] args) 
 5     {
 6         int age;
 7         System.out.print("请输入年龄:");
 8         Scanner sc=new Scanner(System.in);
 9         age=sc.nextInt();
10         if(age<13)
11         {
12             System.out.println("儿童");
13         }
14         else if(age<20)
15         {
16             System.out.println("少年");
17         }
18         else if(age<40)
19         {
20             System.out.println("青年");
21         }
22         else if(age<60)
23         {
24             System.out.println("中年");
25         }
26         else
27         {
28             System.out.println("老年");
29         }
30     }
31 }

 Two, switch statements

  a switch statement to control the expression of labels and, if statements, and the difference is, the back of the switch statement to control the expression of only the data type byte, short, char, int four kinds of integer type, enumerated types and java .lang.String type (7 allowed from the Java) , it can not be a boolean type.

switch syntax :

. 1  Switch (expression The)
 2  {
 . 3      Case condition1:
 . 4      {
 . 5          code blocks. 1
 . 6          BREAK ;
 . 7      }  
 . 8      Case condition2 The:
 . 9      {
 10          block 2
 . 11          BREAK ;
 12 is      } 
 13 is  ...
 14          Case conditionN:
 15      {
 16          block N
 . 17          BREAK ;
 18 is      } 
 . 19      default :
 20 is      {
21          block
 22      }
 23 }      
View Code

 Example:

. 1  Import . Classes in java.util * ;
 2  class   SwitchTest
 . 3  {
 . 4      public  static  void main (String [] args) 
 . 5      {
 . 6          of System.out.print ( "Please enter the score grade:" );
 7          // declare variables Score 
. 8          Scanner = SC new new Scanner (the System.in);
 . 9          char Score = sc.next () the charAt (0. );
 10          Switch (Score)
 . 11          {
 12 is          Case 'A' :
 13 is              System.out.println ( "excellent" );
14             break;
15         case 'B':
16             System.out.println("良");
17             break;
18         case 'C':
19             System.out.println("中");
20             break;
21         default:
22             System.out.println("不及格");
23         }
24     }
25 }
View Code

 Java11 compiler made some improvements, if the developers forgot break the back of the case statement block, Java11 compiler generates a warning: "[fallthrough] may not be possible case"

Java 7 began enhances the functionality of the switch statement allows controlling expression java.lang.String type of a variable or expression of a switch statement - can only be java.lang.String type, can not be a StringBuffer or StringBuilder two strings Types of.

. 1  class   StringSwitchTest
 2  {
 . 3      public  static  void main (String [] args) 
 . 4      {
 . 5          // declare variables Season 
. 6          var Season = "summer" ;
 . 7          Switch (Season)
 . 8          {
 . 9          Case "spring" :
 10              the System.out. println ( "spring" );
 . 11              BREAK ;
 12 is          Case "summer" :
 13 is          System.out.println ( "hot summer" );
 14          BREAK ;
 15         Case "fall" :
 16          System.out.println ( "cool autumn" );
 . 17          BREAK ;
 18 is          Case "winter" :
 . 19          System.out.println ( "winter snow" );
 20 is          BREAK ;
 21 is          default :
 22 is              the System .out.println ( "season input error" );
 23          }
 24      }
 25 }
View Code

Guess you like

Origin www.cnblogs.com/weststar/p/12297527.html