for循环嵌套: for (; x < 15; x++) for (char[] c = new char[1024]; (length = r.read(c)) != -1;)

来源:互联网 发布:双色球篮球 算法 编辑:程序博客网 时间:2024/05/03 21:32

  就我所知道的编程语言中都有循环语句: for, while 及 do...while,在这里要说的就是他们的区别,我不喜欢用语言来说,大家看代码:coding.............

       while和do...while区别:

          while:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class xunhuanTest {  
  2. <span style="white-space:pre">    </span>public static void main(String args[]){  
  3. <span style="white-space:pre">    </span>      int x = 10;  
  4. <span style="white-space:pre">    </span>      //先判断再循环,判断x是否小于9,成立循环,不成立不循环  
  5. <span style="white-space:pre">    </span>      while( x < 9 ){  
  6. <span style="white-space:pre">    </span>         System.out.print("value of x : " + x );  
  7. <span style="white-space:pre">    </span>         x++;  
  8. <span style="white-space:pre">    </span>         System.out.print("\n");  
  9. <span style="white-space:pre">    </span>      }  
  10. <span style="white-space:pre">    </span>   }  
  11. }  
       运行结果:

            无结果

         do...while:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class xunhuanTest {  
  2.     public static void main(String args[]){  
  3.           int x = 10;  
  4.           //先循环在判断。首先循环一次,输出value of x : 10,在判断x是否小于9,成立则循环,不成立不循环  
  5.           do{  
  6.              System.out.print("value of x : " + x );  
  7.              x++;  
  8.              System.out.print("\n");  
  9.           }while( x < 9 );  
  10.        }  
  11. }  

        运行结果:

          value of x : 10

从上两个例子中,可以看出 while和do...while区别:

        while先判断再循环,do...while先循环在判断,即至少执行一次。

在实际编码操作中,用的最多的还是for循环:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class xunhuanTest {  
  2.     public static int x = 10;  
  3.   
  4.     public static void main(String args[]) {  
  5.         // for循环,循环出10-14之间的值(包括10和14)  
  6.         for (x = x; x < 15; x++) {  
  7.             System.out.print("value of x : " + x);  
  8.             System.out.print("\n");  
  9.         }  
  10.     }  
  11. }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class xunhuanTest {  
  2.     public static int x = 10;  
  3.   
  4.     public static void main(String args[]) {  
  5.         // for循环,循环出10-14之间的值(包括10和14)  
  6.         for (; x < 15; x++) {  
  7.             System.out.print("value of x : " + x);  
  8.             System.out.print("\n");  
  9.         }  
  10.     }  
  11. }<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,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程。
for循环嵌套:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class xunhuanTest {  
  2.     public static int x = 10;  
  3.     public static int y = 10;  
  4.     public static void main(String args[]) {  
  5.         // for循环,循环出10-14之间的值(包括10和14)  
  6.         for (; x < 15; x++) {              
  7.             for(;y<15;y++){  
  8.                 System.out.print("value of x : " + x);  
  9.                 System.out.print("value of y : " + y);  
  10.                 System.out.print("\n");  
  11.             }             
  12.         }  
  13.     }  
  14. }  
运行结果:
value of x : 10value of y : 10
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循环

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class Test {  
  2.   
  3.    public static void main(String args[]){  
  4.       int [] numbers = {1020304050};  
  5.   
  6.       for(int x : numbers ){  
  7.          System.out.print( x );  
  8.          System.out.print(",");  
  9.       }  
  10.       System.out.print("\n");  
  11.       String [] names ={"James""Larry""Tom""Lacy"};  
  12.       for( String name : names ) {  
  13.          System.out.print( name );  
  14.          System.out.print(",");  
  15.       }  
  16.    }  
  17. }  
以上实例编译运行结果如下:
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");      }   }}

以上实例编译运行结果如下:

1020

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");      }   }}

以上实例编译运行结果如下:

10204050
  如果你想了解if语句和ifelse语句请看:
       http://www.w3cschool.cc/java/java-if-else-switch.html





      

   

0 0
原创粉丝点击