前言
顺序结构的程序语句只能被执行一次。
如果您想要同样的操作执行多次,就需要使用循环结构。
Java中有三种主要的循环结构:
- while 循环
- do…while 循环
- for 循环
一、while 循环
while是最基本的循环,它的结构为:
while( 布尔表达式 ) {
//循环内容
}
只要布尔表达式为 true,循环就会一直执行下去。
public class WhileDemo5 {
public static void main(String[] args) {
// 只要布尔表达式为 true,循环就会一直执行下去。
System.out.println("----Hello World----");
int i = 0;
while (i < 3) {
System.out.println("Hello World");
i++;
}
System.out.println("----value of x----");
int x = 10;
while( x < 15 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
System.out.println("----死循环----");
int j = 0;
while (j < 3) { //如果没有迭代语句j++,j永远是0,会一直跑下去,成为死循环
System.out.println("Hello Java");
//j++;
}
}
}
编译运行结果如下:
----Hello World----
Hello World
Hello World
Hello World
----value of x----
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
----死循环----
Hello Java
Hello Java
Hello Java
Hello Java
Hello Java
Hello Java
······
二、do…while 循环
对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。
do {
//代码语句
}while(布尔表达式);
注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。
public class DoWhileDemo7 {
public static void main(String[] args) {
System.out.println("----do...while循环----");
int i = 0;
do {
System.out.println("Hello World");
i++;
} while (i < 3);
//变量在while循环后还可以使用
System.out.println("现在变量i = " + i);
}
}
编译运行结果如下:
----do...while循环----
Hello World
Hello World
Hello World
现在变量i = 3
三、for 循环
虽然所有循环结构都可以用 while 或者 do…while表示,但 Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单。
for循环执行的次数是在执行前就确定的。语法格式如下:
for(初始化; 布尔表达式; 更新) {
//代码语句
}
关于 for 循环有以下几点说明:
- 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量。
- 再次检测布尔表达式。循环执行上面的过程。
public class ForDemo1 {
public static void main(String[] args) {
// for(初始化语句;循环条件;迭代语句)
// for循环快捷键fori
// 最常用
System.out.println("----3次Hello World----");
for (int i = 0; i < 3; i++) {
//i = 0,i = 1,i = 2
System.out.println("Hello World");
}
System.out.println("----4次Hello MySQL----");
for (int i = 1; i < 5; i++) {
//i = 1,i = 2,i =3,i = 4
System.out.println("Hello MySQL");
}
System.out.println("----5次Hello Spring----");
for (int i = 1; i <= 5; i++) {
//i = 1,i = 2,i = 3,i = 4,i = 5
System.out.println("Hello Spring");
}
System.out.println("----3次Hello Redis----");
for (int i = 1; i <= 5; i += 2) {
//i = 1,i = 3,i = 5
System.out.println("Hello Redis");
}
}
}
编译运行结果如下:
----3次Hello World----
Hello World
Hello World
Hello World
----4次Hello MySQL----
Hello MySQL
Hello MySQL
Hello MySQL
Hello MySQL
----5次Hello Spring----
Hello Spring
Hello Spring
Hello Spring
Hello Spring
Hello Spring
----3次Hello Redis----
Hello Redis
Hello Redis
Hello Redis
1.增强 for 循环
Java5 引入了一种主要用于数组的增强型 for 循环。
Java 增强 for 循环语法格式如下:
for(声明语句 : 表达式)
{
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
public class ForDemo2 {
public static void main(String[] args){
/*
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。
其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
*/
int [] numbers = {10, 20, 30, 40, 50};
// 声明语句int x
// 表达式numbers
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"James", "Larry", "Tom", "Lacy"};
// 声明语句String name
// 表达式names
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
编译运行结果如下:
10,20,30,40,50,
James,Larry,Tom,Lacy,
四、break和continue关键字
1.break关键字
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。
break 的语法很简单,就是循环结构中的一条语句:
break;
使用方法:
public class BreakAndContinueDemo10 {
public static void main(String[] args) {
System.out.println("----正常循环----");
for (int i = 0; i < 4; i++) {
System.out.println("正常洗碗");
}
System.out.println("~~~~使用break关键字~~~~");
// break 跳出并结束当前循环的执行
// 场景:你老婆罚你做4天家务,每天都是洗碗。
// 但是洗碗到第3天后心软了 原谅你了不用洗了
for (int i = 0; i < 4; i++) {
System.out.println("快乐的洗碗~~~~");
//i = 0,i = 1,i = 2
if (i == 2) {
System.out.println("不洗碗了!!!");
break; // 跳出并结束当前循环的执行~~
}
}
}
}
编译运行结果如下:
----正常循环----
正常洗碗
正常洗碗
正常洗碗
正常洗碗
~~~~使用break关键字~~~~
快乐的洗碗~~~~
快乐的洗碗~~~~
快乐的洗碗~~~~
不洗碗了!!!
2.continue关键字
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
continue 语法就是循环体中一条简单的语句:
continue;
使用方法:
public class BreakAndContinueDemo10 {
public static void main(String[] args) {
System.out.println("~~~~使用continue关键字~~~~");
// continue 跳出当前循环的当次执行,进入循环的下一次
// 场景:假如你又有老婆了,然后你犯错了,你老婆罚你做5天家务,
// 每天都是洗碗。但是洗碗到第三天后心软了 原谅你了不用洗了
// 但是依然不解恨 继续洗第4天 5天
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("今天不洗了");
continue; // 立即跳出当次执行,进入循环的下一次!
}
System.out.println("洗碗第" + i + "天");
}
}
}
编译运行结果如下:
~~~~使用continue关键字~~~~
洗碗第1天
洗碗第2天
今天不洗了
洗碗第4天
洗碗第5天