22.循环语句
while循环
-
while循环时最基本的循环结构
while(布尔表达式){
//循环执行语句
}
- 大部分的while循环我们都会让它停下来,只有部分情况我们会让循环一直执行,例如:服务器响应监听,事件监听等。
- 我们正常的业务逻辑编程中应该尽量避免死循环,会降低程序的性能或者造成程序卡死崩溃
package com.demo.Cycle;
public class Demo1 {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println(sum);
}
}
do…while 循环
- do while循环不管循环条件成不成立至少会执行一次,先执行,后判断
package com.demo.Cycle;
public class Demo2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i < 0);
}
}
for循环
-
for循环语句是支持迭代的一种通用结构,是最灵活的,最有效的循环
-
for循环执行的次数,在执行前就已经确定了
for(初始化(表达式1);布尔表达式(表达式2);更新(表达式3)){
//循环体执行语句
}
-
三个案例解析for循环
- 求100以内(包括100)的偶数
package com.demo.Cycle; public class Demo3 { public static void main(String[] args) { for (int i = 0; i <= 100; i++) { if (i % 2 == 0) { System.out.println(i); } } } }
- 求水仙花数(每个位的数字三次方之和等于它本身,例:153 = 13+53+33)
package com.demo.Cycle; public class Demo4 { public static void main(String[] args) { int bai, shi, ge; // System.out.println(321 % 10); // 去左边 1 // System.out.println(521 / 10); // 去右边 52 for (int i = 100; i < 1000; i++) { bai = i / 100; shi = i / 10 % 10; ge = i % 10; if (i == Math.pow(bai, 3) + Math.pow(shi, 3) + Math.pow(ge, 3)) { System.out.println(i); } } } }
- 打印九九乘法表
package com.demo.Cycle; public class Demo5 { public static void main(String[] args) { for (int row = 1; row <= 9; row++) { for (int cls = 1; cls <= row; cls++) { System.out.print((cls + "*" + row) + "=" + cls * row + "\t"); } System.out.println(); } } }
增强for循环
- 为可迭代结构而生
for(声明语句:表达式)
{
// 循环语句
}
- 可迭代结构例如数组,可以遍历的结构,内部有一个先后顺序,可以通过索引获取里边的元素
package com.demo.Cycle;
public class Demo6 {
public static void main(String[] args) {
int[] nums = {15, 6, 58, 95, 31, 52}; // 定义一个数组
// 遍历数组元素
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
System.out.println("=========================");
// 遍历数组元素
for (int num : nums
) {
System.out.println(num);
}
}
}
控制循环的关键字
- break 可以直接跳出当前这一层循环
- 如果需要直接跳出最外层循环需要使用标签作为标记
package com.demo.Cycle;
public class Demo7 {
public static void main(String[] args) {
int i = 0;
while (i < 100) {
i++;
System.out.println(i);
if (i == 30) {//到30 就终止循环
break;
}
}
System.out.println("============================");
i = 0;
while (i < 100) {
i++;
System.out.println(i);
for (int j = 0; j < 10; j++) {
if (i > 2) break;//跳出当层for循环,并不会对外层循环造成影响
}
}
System.out.println("============================");
i = 0;
a:// 标记当前位置
while (i < 100) {
i++;
System.out.println(i);
for (int j = 0; j < 10; j++) {
if (i > 2) break a;//跳出指定循环,此时会直接跳出while循环
}
}
}
}
- continue 可以跳过本次循环
package com.demo.Cycle;
public class Demo8 {
public static void main(String[] args) {
for (int i = 0; i < 100 ; i++) {
if (i%2==0){
continue; // 跳出当次循环后边的语句将不再执行而直接进入循环判断,所以所有偶数都不会被输出
}
System.out.println(i);
}
}
}
-
continue也支持标签作为标记跳转(逻辑过于复杂,不便于理解代码,不推荐使用)
- 求101-150之间的质数
package com.demo.Cycle; public class Demo9 { public static void main(String[] args) { // 打印 101-150之间的质数 label: for (int i = 101; i < 150; i++) { for (int j = 2; j < i / 2; j++) { if (i % j == 0) continue label; } System.out.print(i + " "); } } }