java 控制流

一:

    块作用域

块(即复合语句):是指由一对花括号括起来的若干条简单的java语句。块决定了变量的作用域,一个块可以嵌套在另一个块中,如下:

1 public class print_In {
2     public static void main(String[] args){
3     int a=2;
4         {
5             int a=3;
6             int b=3;
7         }
8     }
9 }

 需要注意:在一个块里,定义个变量,不能再嵌套的块里在定义相同的变量,如果定义会报错。

二:if语句

语法形式和js是一样:if(){}else if (){} ......else{}

java里的if语句不同于python里的if语句,python里可以直接用数组、字符串、字典等来充当条件,但是在java里只能使用表达式直接可以判断布尔值,才可以做判断,否则会报错。

 1 public class print_In {
 2     public static void main(String[] args){
 3     int a=2;
 4     int v=4;
 5     if (a<4){
 6         System.out.println("OK");
 7     }else {
 8         System.out.println("bad");
 9     }
10     }
11 }

 错误的写法:

 1 public class print_In {
 2     public static void main(String[] args){
 3     int a=2;
 4     int v=4;
 5     if (a){
 6         System.out.println("OK");
 7     }else {
 8         System.out.println("bad");
 9     }
10     }
11 }

for循环:

语法:for (){} 通过计数器或者变量的值修改。来结束for循环,()表达式里包含三部分,初始化一个变量并赋值,变量的布尔比较,变量的自增。目的是循环的跳出。变量的作用域,作用域循环体。

如下:

1 public class print_In {
2     public static void main(String[] args) {
3         int i;
4         for(i=2;i<4;i++){
5             System.out.println(i);
6         }
7         System.out.printf("i value is %s ",i);
8     }
9 }

 

如果main方法想使用for循环之后的变量值,需要在for循环外定义这个变量。才可以被外部调用!

 while 语句:

形式:while(condition){statement} 这种形式,当condition 成立的时候,才运行块代码,如果不成立将不会运行。

1 public class  print_In{
2     public  static  void  main(String[]  args){
3         int i=3;
4         while (i<6){
5             System.out.println(i);
6             i+=1;
7         }
8     }
9 }

 

while 循环另一个形式: do  {statement}while(condition)会运行一次块代码,在做判断。

 1 public class  print_In{
 2     public  static  void  main(String[]  args){
 3         int i=3;
 4         do {
 5 
 6                 System.out.println(i);
 7 
 8         }while (i<3);
 9     }
10 }

 

这种场景比较多见的是:交互的时候,需要输出一定条件,然用户去做判断。

switch 多个选项:(比较少用)

当我们做多个判断的时候,使用if/else的时候显得很臃肿,可以使用switch case语句来判断。结构:

switch(var){case var:statement break;case var:statement break。。。。。。}注意加break 避免执行多个case分支。

 1 import java.util.Scanner;
 2 
 3 public class  print_In{
 4     public  static  void  main(String[]  args){
 5         Scanner scan= new Scanner(System.in);
 6         System.out.println("Please entre your choice?");
 7         int  chocie=scan.nextInt();
 8         switch (chocie){
 9             case 1:
10                 System.out.printf("you have entre %d",chocie);
break
11 case 2: 12 System.out.printf("you have entre %d",chocie);
break
13 case 3: 14 System.out.printf("you have entre %d",chocie);
break
15 } 16 } 17 }

 

需要注意的是case后面跟的choice的值!!!

如果用户没有输入没有匹配的值,在if语句中我们使用else,在switch里用default子语句来判断未匹配到选项值的处理。default子句也需要break。

 

 1 import java.util.Scanner;
 2 
 3 public class  print_In{
 4     public  static  void  main(String[]  args){
 5         Scanner scan= new Scanner(System.in);
 6         System.out.println("Please entre your choice?");
 7         int  chocie=scan.nextInt();
 8         switch (chocie){
 9             case 1:
10                 System.out.printf("you have entre %d",chocie)
break;
11 case 2: 12 System.out.printf("you have entre %d",chocie)
break;
13 case 3: 14 System.out.printf("you have entre %d",chocie)
break;
15 default: 16 System.out.printf("bad input");
break
17 } 18 } 19 }

 

 

四:中断流程

在java中,和python一样也有break和continue,但是java中的break 可以带标签进行退出。而python 不可以。

 1 public class loop {
 2     public static  void main(String[] args){
 3         int i=2;
 4         while (i<5){
 5             System.out.printf("i is %d \n",i);
 6             if (i==4) break;
 7             i+=1;
 8         }
 9     }
10 }

 

其中if(condition) break;为缩写,也可以写下面这样:

 1 public class loop {
 2     public static  void main(String[] args){
 3         int i=2;
 4         while (i<5){
 5             System.out.printf("i is %d \n",i);
 6             if (i==4){
 7                 break;
 8             }
 9             i+=1;
10         }
11     }
12 }

带标签的break tag  ,其中tag需要用冒号,声明。指定跳到那个标签。如下:

 1 public class loop {
 2     public static  void main(String[] args){
 3         int i=3;
 4         break_tag:
 5         while ( i<6){
 6             System.out.printf("i is %d\n" ,i);
 7             for (int a=2;a<6;a++){
 8                 if (a==4){
 9                     break  break_tag;
10                 }
11                 System.out.printf("a is %d\n",a);
12             }
13             i++;
14         }
15     }
16 }

 

注意语法:tag后面是冒号: ,紧随循环体,否则报没有块!

跳到tag之后,不会再次执行跳出的块代码(循环或者块的代码),执行后面的语句。

 1 public class loop {
 2     public static  void main(String[] args){
 3         int i=3;
 4         break_tag:
 5         while ( i<6){
 6             System.out.printf("i is %d\n" ,i);
 7             for (int a=2;a<6;a++){
 8                 if (a==4){
 9                     break  break_tag;
10                 }
11                 System.out.printf("a is %d\n",a);
12             }
13             i++;
14         }
15         System.out.printf("OK");
16     }
17 }

tag的位置。tag可以跳出多个循环。但是break只能跳出块,不能跳入块!

当然了,不仅仅是loop情况,也可以是if等语句的块代码。

 1 public class loop {
 2     public static  void main(String[] args){
 3         int i=3;
 4             for (int a=2;a<6;a++){
 5                 break_tag:
 6                 if(i >0){
 7 
 8                 if (a==4){
 9                     break  break_tag;
10                 }
11                 System.out.printf("a is %d\n",a);
12             }
13                 i++;
14             }
15     }
16 }

 输出结果:

1 a is 2
2 a is 3
3 a is 5

 

continue 和python 里的continue 意思是一样。就是跳出循环的,回到循环首部。

当然continue也有标签的意思。指定跳到匹配的标签的循环首部。

 

posted @ 2017-09-25 17:42  evil_liu  阅读(698)  评论(0编辑  收藏  举报