Chapter4 执行顺序控制(Controlling Execution)

0. 基础关键词(basic control keywords)

  • for, while, do-while, if-else if-else, continue, break,switch, return等常用控制关键词

  • 逗号(Comma operator):在流程控制中,只出现在for循环的初始化和step部分,并且各部分顺序执行,可以从下面的示例结果中看到i和j是顺序执行的。需要注意在初始化部分使用逗号时需要保证变量类型相同,即如果在for初始化部分新定义一个String s变量程序会报错。

    示例如下:

    public class CommaTest1 {
    	 public static void main(String[] argv){
    		for(int i=0,j=5;i<5;i+=2,j=2*i) {
    			System.out.println("i,j = "+i+","+j);
    		}
    	 }
    }
    

    Output:

    i,j = 0,5

    i,j = 2,4

    i,j = 4,8

1. Foreach syntax

  • foreach语法是一种新的for循环语法,目的是为了遍历数组或者java中的container。相比于常规的for语法,foreach的可读性更高,因为我们不需要再定义变量i,这显然更符合常识,但在使用时还是需要声明遍历的元素类型。

    示例如下:

    public class ForEachTest1 {
    	 public static void main(String[] argv){
    		Random rand = new Random(22);
    		int[] arr = new int[5];
    		for(int i=0;i<5;i++) {
    			arr[i] = rand.nextInt(10);
    		}
    		for(int a:arr) { //foreach syntax
    			System.out.println(a);
    		} 
    	 }
    }
    

    Output:

    2

    0

    2

    1

    9

2. Java中的标签(labels)

  • 在java中标签的唯一用法是当存在多层循环的时候,可以利用标签进行continue或break到标签所在的循环,否则每次只能跳出最近的循环。这是一种比较有意思的用法,有点类似于汇编语言中的JUMP,但java的标签只能用于循环的控制,也就是说只能出现在for,while等循环体之前(也可以标签叠加,使得出现在另一个标签之前,但没有实际意义)。

  • 用法如下:

    label1:

    Outer-iteration{

    ​ inner-iteration{

    ​ //…

    ​ break;

    ​ //…

    ​ continue;

    ​ //…

    ​ continue label1;

    ​ //…

    ​ break label1;

    }}

  • 示例如下:

    public class LabelTest1 {
    	 public static void main(String[] argv){
    		 int count = 5;
    		 OUTER://outer-label
    			 for(int i=0;i<count;i++) {
    				 for(int j=0;j<count;j++) {
    					if(j==2 && i==3) break OUTER; //no lable: if(j==2 && i==3) break; the output would be different
    					if(j==4) break;
    					System.out.println("i,j = "+i+","+j);
    				}
    			 }
    	}
    }
    

    Output:

    i,j = 0,0

    i,j = 0,1

    i,j = 0,2

    i,j = 0,3

    i,j = 1,0

    i,j = 1,1

    i,j = 1,2

    i,j = 1,3

    i,j = 2,0

    i,j = 2,1

    i,j = 2,2

    i,j = 2,3

    i,j = 3,0

    i,j = 3,1

3. Switch

  • switch的基本用法如下,需要注意的是可以省略case中的statement和break,那么程序就会顺序执行判断,只要selector和某一项相等就表示条件满足。这里的selector可以是整数值,也可以是String等类型,当selector是String时,会自动对内容进行比较,而不是reference。(之后会提到enum类型也可以作为switch中的值)

    switch(selector){

    case value1: statement; break;

    case value2: statement; break;

    case valuen: statement; break;

    default: statement;

    }

  • 示例如下:

    public class SwitchTest1 {
    	 public static void main(String[] argv){
    		 Random rand = new Random(11);
    		 for(int i=0;i<6;i++) {
    			 int ch = 'a' + rand.nextInt(26);
    			 switch(ch){
    			 case 'a':
    			 case 'b':	
    			 case 'c':	System.out.println("im one of the first three characters. ---"+(char)ch);
    			 			break;
    			 default:	System.out.println("im not a/b/c. ---"+(char)ch);
    
    			 }
    		 }
    	}
    }
    

    Output:

    im not a/b/c. —o

    im not a/b/c. —m

    im not a/b/c. —v

    im not a/b/c. —l

    im one of the first three characters. —b

    im not a/b/c. —h

猜你喜欢

转载自blog.csdn.net/qq_32483589/article/details/87924741