就我所知道的编程语言中都有循环语句: for, while 及 do...while,在这里要说的就是他们的区别,我不喜欢用语言来说,大家看代码:coding.............
while和do...while区别:
while:
- public class xunhuanTest {
- <span style="white-space:pre"> </span>public static void main(String args[]){
- <span style="white-space:pre"> </span> int x = 10;
- <span style="white-space:pre"> </span> //先判断再循环,判断x是否小于9,成立循环,不成立不循环
- <span style="white-space:pre"> </span> while( x < 9 ){
- <span style="white-space:pre"> </span> System.out.print("value of x : " + x );
- <span style="white-space:pre"> </span> x++;
- <span style="white-space:pre"> </span> System.out.print("\n");
- <span style="white-space:pre"> </span> }
- <span style="white-space:pre"> </span> }
- }
无结果
do...while:
- public class xunhuanTest {
- public static void main(String args[]){
- int x = 10;
- //先循环在判断。首先循环一次,输出value of x : 10,在判断x是否小于9,成立则循环,不成立不循环
- do{
- System.out.print("value of x : " + x );
- x++;
- System.out.print("\n");
- }while( x < 9 );
- }
- }
运行结果:
value of x : 10
从上两个例子中,可以看出 while和do...while区别:
while先判断再循环,do...while先循环在判断,即至少执行一次。
在实际编码操作中,用的最多的还是for循环:
- public class xunhuanTest {
- public static int x = 10;
- public static void main(String args[]) {
- // for循环,循环出10-14之间的值(包括10和14)
- for (x = x; x < 15; x++) {
- System.out.print("value of x : " + x);
- System.out.print("\n");
- }
- }
- }
- public class xunhuanTest {
- public static int x = 10;
- public static void main(String args[]) {
- // for循环,循环出10-14之间的值(包括10和14)
- for (; x < 15; x++) {
- System.out.print("value of x : " + x);
- System.out.print("\n");
- }
- }
- }<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </strong>
运行结果都为:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
可以看出for循环特点:
- 最先执行初始化步骤。可以声明并初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量。
- 再次检测布尔表达式。循环执行上面的过程。
- public class xunhuanTest {
- public static int x = 10;
- public static int y = 10;
- public static void main(String args[]) {
- // for循环,循环出10-14之间的值(包括10和14)
- for (; x < 15; x++) {
- for(;y<15;y++){
- System.out.print("value of x : " + x);
- System.out.print("value of y : " + y);
- System.out.print("\n");
- }
- }
- }
- }
value of x : 10value of y : 11
value of x : 10value of y : 12
value of x : 10value of y : 13
value of x : 10value of y : 14
Java增强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 ={"James", "Larry", "Tom", "Lacy"};
- for( String name : names ) {
- System.out.print( name );
- System.out.print(",");
- }
- }
- }
10,20,30,40,50, James,Larry,Tom,Lacy,可以看出:
for(声明语句 : 表达式) { //代码句子 }
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
再来看一下break continue 关键字break关键字
break主要用在循环语句或者switch语句中,用来跳出整个语句块。
break跳出最里层的循环,并且继续执行该循环下面的语句。
语法
break的用法很简单,就是循环结构中的一条语句:
break;
实例
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { break; } System.out.print( x ); System.out.print("\n"); } } }
以上实例编译运行结果如下:
10 20
continue关键字
continue适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在for循环中,continue语句使程序立即跳转到更新语句。
在while或者do…while循环中,程序立即跳转到布尔表达式的判断语句。
语法
continue就是循环体中一条简单的语句:
continue;
实例
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"); } } }
以上实例编译运行结果如下:
10 20 40 50