Java基础笔记(九)—— 流程控制

Java三大流程控制语句:顺序、选择、循环。

if结构、if-else结构、多重if、嵌套if。

 1 public class Test {
 2         public static void main(String[] args){
 3             int a=1;
 4             int b=1;
 5             if (a==b)
 6                 a++;   //不加大括号,只能有一句语句
 7             else
 8                 a--;
 9 
10 
11             if(a==b){
12                 a++;
13                 b++;
14             }else{
15                 a--;
16             }
17 
18 
19             if(a<10)        //多重if结构
20                 a++;
21                 else if(a<20)
22                     a--;
23                     else if(a<30)
24                         b--;
25                     else
26                         a*=10;
27 
28 
29             if(a==b){  //嵌套if结构,将整个if块插入另一个if块中
30                 if(a>0)
31                     if(a==1)
32                         a++;
33             }else{
34                 b+=10;
35             }
36 
37         }
38 }

if结构:判断条件是布尔类型,是一个范围。

switch结构:判断条件是常量值。

 1 public class Test {
 2         public static void main(String[] args){
 3            int x=4;
 4            switch (x){
 5                case 1:System.out.println(x);
 6                case 2:System.out.println(x);
 7                case 3:System.out.println(x);
 8                case 4:System.out.println(x);   //运行结果444
 9                case 5:System.out.println(x);
10                default:System.out.println(x);
11            }
12            System.out.println("-----------------------------------------------");
13             switch (x){
14                 case 1:System.out.println(x);
15                        break;
16                 case 2:System.out.println(x);
17                        break;
18                 case 3:System.out.println(x);
19                        break;
20                 case 4:System.out.println(x);   //运行结果4
21                        break;
22                 case 5:System.out.println(x);
23                        break;
24                 default:System.out.println(x);
25             }
26         }
27 }

switch用于多分支选择,代码中的x可以是int 型或能自动转换为int型的变量、final定义的下述类型常量或数值(包括整数或字符)、整数表达式(含有变量或常量均可),包括:byte、short、char、int以及它们对应的包装类(Java7后支持String类型)

为什么呢?因为byte、short、char类型的值能被隐式地转换为int型,long、float、double类型不能隐式地转换为int型,因此不能作为switch或case的常量参数或常量表达式中,除非对其强转为int型。

如:switch(1)        switch(a)[假设int a=1; short a=1;  final int a=1;...]              switch('a')         switch(2+1)             switch(a+2)[假设a已被定义]

case后对应其常量值、整数表达式(不能出现变量,即使是final型也不可以,只能是整数数字)、final型的常量、字符串   

如:case 1:            case 'a':              case 1+1:               case z: [假设final int z=1;]                  case "hello":

switch对字符串的支持,也是因为int型值的匹配。对case后的String对象调用hashCode()方法,会得到一个int型的hash值,用此hash值唯一标识这个case。匹配时首先调用这个字符串的hashCode()函数,获取到一个hash值,用这个hash值去匹配所有case,若没有匹配到,说明不存在;若匹配成功,接下来调用字符串的String.equals()方法进行匹配。因此,String变量、case子句的字符串都不要为null。

如果不想要匹配后的case后的语句都输出,不要忘记在每句后加break;

 1 public class Test {
 2         public static void main(String[] args){
 3           int a=1;
 4           int i=0;
 5           while(a==1){
 6               a++;
 7               System.out.println(a);  //2
 8           }
 9           do{
10               a++;
11           }while(a==1);
12           System.out.println(a);    //3
13           for( i=0;i<3;i++){
14               for(int j=0;j<=i;j++){
15                     System.out.println(j);  //0 01 012
16               }
17           }
18           System.out.println(i);     //3
19         }
20 }

可互相嵌套循环。for循环三个表达式均可省略,注意for循环表达式的执行顺序,表达式2,中间体,表达式3,再表达式2比较,...

对于break、continue、return:

break是跳出当层循环;continue是跳出当次循环,此层循环中continue后面的语句不再执行,回到循环起始处继续执行;return是使程序控制返回到调用该方法的地方,当执行main方法时,return可使程序执行返回到Java运行系统。

猜你喜欢

转载自www.cnblogs.com/tendo/p/10357665.html