Java基础(三)选择和循环结构

一、选择结构,条件判断

1、if 语句

  一个 if 语句包含一个布尔表达式和一条或多条语句。如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码。

 1 import static java.lang.Math.round;
 2 
 3 public class Conditional {
 4     /**
 5      * 简单if语句
 6      */
 7     @Test
 8     public void testIf() {
 9         double random = Math.random();
10         int num = (int) round(random * 10);  //创建一个随机整数
11         System.out.println(num);
12         if (num < 3) {
13             System.out.println("num小于3");
14         } else {
15             System.out.println("num大于等于3");
16         }
17     }
18 
19     /**
20      * 简单if, else if,else语句
21      */
22     @Test
23     public void testIf2() {
24         double random = Math.random();
25         int num = (int) round(random * 10);  //创建一个随机整数
26         System.out.println(num);
27         if (num < 3) {
28             System.out.println("num小于3");
29         } else if(num < 6) {
30             System.out.println("num小于6");
31         }else {
32             System.out.println("num大于等于6");
33         }
34     }
35 
36     /**
37      * 嵌套if语句
38      */
39     @Test
40     public void testIf3() {
41         double random = Math.random();
42         int num = (int) round(random * 10);  //创建一个随机整数
43         System.out.println(num);
44         if (num < 3) {
45             System.out.println("num小于3");
46         } else if(num < 6) {
47             System.out.println("num小于6");
48         }else {
49             if(num == 6){
50                 System.out.println("num等于6");
51             }
52             System.out.println("num大于6");
53         }
54     }
55 }

2、switch 语句

  switch 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

语法:

switch(变量或者一个表达式){
case 变量的可能值1: 功能语句;
break; case 变量的可能值2: 功能语句;
break; case 变量的可能值3: 功能语句;
break; ........ default:功能语句; }

switch 语句规则:

  • switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串类型了

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 后面值的数据类型必须和变量的数据类型一致,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

  • 按照case的值的大小顺序排列,default放最后,default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

 1 import static java.lang.Math.round;
 2 
 3 public class Conditional1 {
 4     /**
 5      * switch语句
 6      */
 7     @Test
 8     public void testSwitch(){
 9         double random = Math.random();
10         int num = (int) round(random * 10);
11         System.out.println(num);
12         switch(num){
13             case 0 :
14                 System.out.println("num为0");
15                 break;
16             case 1 :
17                 System.out.println("num为1");
18                 break;
19             case 2 :
20                 System.out.println("num为2");
21                 break;
22             case 3 :
23                 System.out.println("num为3");
24                 break;
25             case 4 :
26                 System.out.println("num为4");
27                 break;
28             case 5 :
29                 System.out.println("num为5");
30                 break;
31             default :
32                 System.out.println("num大于5");
33         }
34     }
35 }

二、循环结构

1、while循环

  先判断条件,再执行语句

2、do-while循环

  先执行一次,再判断条件

 1 public class Circulation {
 2     /**
 3      * while循环语句
 4      * 先判断,再执行
 5      */
 6     @Test
 7     public void testWhile() {
 8         int num = 5;
 9         while (num > 0) {
10             System.out.println(num);
11             num -= 1;
12         }
13     }
14 
15     /**
16      * do-while循环
17      * 先do执行一次,再判断
18      */
19     @Test
20     public void testDoWhile() {
21         int num = 5;
22         do {
23             System.out.println(num);
24             num -= 1;
25         } while (num > 0);
26     }
27 }

3、for循环

  for循环执行的次数是在执行前就确定的。

语法:

for(初始化语句A ; 条件判断B; 循环后功能语句C){
    //循环体D
}

  执行一次循环后,更新循环控制变量,语句C的作用。然后再次检测布尔表达式。循环执行上面的过程。

 1 public class Circulation {
 2     /**
 3      * 简单for循环
 4      */
 5     @Test
 6     public void testFor() {
 7         int num = 10;
 8         for (int i = 0; i < num; i++) {
 9             System.out.println(i);
10         }
11     }
12 }

4、foreach加强的for循环

  Java5 引入了一种主要用于数组的增强型 for 循环。

作用: 主要是用来遍历数组和集合的;

缺点: 没有索引,凡是涉及有索引相关操作还是用普通for循环,

Java 增强 for 循环语法:

for(源中的数据类型 值e: 源){
   //代码句子,直接使用值e
}
 1 public class Circulation1 {
 2     /**
 3      * foreach循环
 4      */
 5     @Test
 6     public void testForeach() {
 7         int[] num = {10, 20, 30, 40, 50};
 8         for (int i : num) {
 9             System.out.println(i);
10         }
11     }
12 }

5、嵌套循环

 1 public class Circulation {
 2     /**
 3      * 嵌套循环实现冒泡排序
 4      */
 5     @Test
 6     public void test() {
 7         int[] num = {12, 2, 7, 5, 14};
 8         int t = 0;
 9         for (int i = 0; i < num.length-1; i++) {
10             for (int j = 0; j < num.length-1; j++){
11                 if (num[j] > num[j + 1]) {
12                     t = num[j];
13                     num[j] = num[j + 1];
14                     num[j + 1] = t;
15                 }
16             }
17         }
18         System.out.println(Arrays.toString(num));
19     }
20 }

三、循环控制语句

1、break

  break 表示终止当前这一层循环,即跳出当前循环。

2、continue

  continue 表示跳过本次循环,进入下一次循环。

3、return

  return 表示结束当前的方法。

注意:break、continue、return 后面都不能跟任何代码,因为永远都不会执行。

for(源中元素类型 e  : 源 ){

直接使用e就可以了

}

原文地址:https://www.cnblogs.com/zt19994/p/8404128.html