本人已参与「新人创作礼」活动,一起开启掘金创作之路。
持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第20天,点击查看活动详情
目录
今天阿文介绍的内容为:
- Java循环结构
- while循环
- do...while循环
- for循环和增强型for循环
Java循环结构
顺序结构的程序语句被执行一次。
如果想要同样的操作执行多次,就需要使用循环结构。
在Java中有三种主要的循环结构:
- while循环
- do...while循环
- for循环
1.while循环
while循环结构:
while (布尔表达式) {
//循环内容
}
只要布尔表达式为true,循环就会一直执行下去
例子:
public class Test {
public static void main(String[] args) {
int x = 1;
while( x < 10 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
输出结果为:
2.do...while循环
对于while语句来说,如果不满足循环条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。
do...while循环结构:
do{
//循环内容
} while(布尔表达式);
注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。
例子:
public class Test {
public static void main(String[] args){
int x = 1;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 10 );
}
}
输出结果为:
3.for循环
当我们知道循环次数的时候,就可以使用Java提供的另一种循环结构 for循环,使一些循环结构变得更加简单。
for循环结构:
for (初始化; 布尔表达式; 更新) {
//循环内容
}
for循环的说明:
- 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量。
- 再次检测布尔表达式。循环执行上面的过程。
例子:
public class Test {
public static void main(String[] args) {
for(int x = 1; x < 10; x++) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
输出结果为:
4.增强for循环
增强for循环结构:
for (声明语句 : 表达式) {
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
例子:
public class Test {
public static void main(String[] args){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names ={"zhangsan", "lisi", "wangwu", "zhaoliu"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
输出结果为:
5.break关键字
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。
例子:
public class Test {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
输出结果为:
5.1 带标签的break
不带标签的break只能跳出包围它的最小代码块,如果想跳出包围它的更外层的代码块,可以使用带标签的break语句。
例子:
public class Text1 {
public static void main(String[] args) {
for (int i = 0; i < 2; i++) {
System.out.println("最外层循环...");
loop: //中层for循环break标签
for (int j = 0; j < 2; j++) {
System.out.println("中层循环...");
for (int k = 0; k < 2; k++) {
System.out.println("最里层循环...");
break loop; //带标签的break,跳出中层循环
}
}
}
}
}
输出结果:
可以看到break跳出了2层循环。
如果不加上break标签则:
输出结果:
6.continue关键字
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
例子:
public class Test {
public static void main(String[] args) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
输出结果为:
总结
- 循环结构只有布尔表达式为true时,才会进行循环
- do...while至少循环
1次 - 增强型for循环使循环变得更简单
- break和continue都是控制循环进程的关键字,但break是直接跳出循环,而continue是结束本次循环进入下一次循环
好了,今天阿文就介绍到这里... byebye