The code in the program is executed sequentially, which means it can only be executed once. If you want to perform the same operation multiple times, the headquarters may copy the code several times! Therefore, a loop structure needs to be used here.
There are three main loop structures in Java:
Examplewhile(true){ //... }Copy after login
The above example compilation and running results are as follows:public class Test { public static void main(String args[]) { int x = 15; while( x < 20 ) { System.out.print("X is : " + x ); x++; System.out.println(""); } } }Copy after loginX is : 15 X is : 16 X is : 17 X is : 18 X is : 19Copy after login
Even if the conditions are not met, the execution statement will be executed once.
do { //代码语句 }while(布尔表达式);Copy after login
Note:ExampleThe Boolean expression is after the loop body, so the statement block has been executed before detecting the Boolean expression. If the Boolean expression evaluates to true, the block of statements executes until the Boolean expression evaluates to false.
The above example compilation and running results are as follows:public class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("值为 : " + x ); x++; System.out.println(""); }while( x < 20 ); } }Copy after login值为 : 10 值为 : 11 值为 : 12 值为 : 13 值为 : 14 值为 : 15 值为 : 16 值为 : 17 值为 : 18 值为 : 19Copy after login
The number of times the for loop is executed is determined before execution. The syntax format is as follows:
There are several instructions about the for loop:for(初始化; 布尔表达式; 操作) { //... //代码语句 //... }Copy after login
public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); } } }Copy after login
以上实例编译运行结果如下:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19Copy after login
Java5 引入了一种主要用于数组的增强型 for 循环。其实学过C#的同学应该知道,这个和foreach循环是一样的。
Java 增强 for 循环语法格式如下:
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。for(声明语句 : 表达式){ //代码句子 }Copy after login
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
这个方法一般会用在对象输出的方式上。
实例
public class Test { public static void main(String args[]){ int [] numbers = {1, 2, 3, 4, 5}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.println(""); String [] names ={"Karl", "DuJinYang", "JiMi", "Lucy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } } }Copy after login
以上实例编译运行结果如下:
10,20,30,40,50, Karl,DuJinYang,JiMi,Lucy,Copy after login
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 中文就是结束的意思,顾名思义,就是跳出最里层(当前涵盖)的循环,并且继续执行该循环下面的语句。
语法
break 的用法很简单,就是循环结构中的一条语句:
break;Copy after login
实例
以上实例编译运行结果如下: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"); } } }Copy after login10 20Copy after login
continue ,继续的意思,适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
语法
continue;Copy after login
实例
以上实例编译运行结果如下: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"); } } }Copy after login10 20 40 50Copy after login
do...while、while、for 循环都是可以相互嵌套的,这里不做过多的演示,大家可以自己去试验一下。
以上就是JAVA 入坑教程 | 章节六 循环结构体的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!