java的for循环和c++的for循环类似
1 public class Test { 2 public static void main(String args[]) { 3 for(int x = 10; x < 20; x = x+1) { 4 System.out.print("value of x : " + x ); 5 System.out.print(" "); 6 } 7 } 8 }
运行输出:
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 : 19
Java 引入了一种主要用于数组的增强型 for 循环。
1 public class Puppy{ 2 public static void main(String[] args){ 3 int[] Nums = {10,20,30,40,50,60}; 4 for(int x : Nums){ 5 System.out.print(x); 6 System.out.print(","); 7 } 8 9 System.out.println(); 10 String[] Names = {"Jacky","Tom","Lily"}; 11 for(String name : Names){ 12 System.out.print(name); 13 System.out.print(','); 14 } 15 } 16 }
运行输出:
10,20,30,40,50,60,
Jacky,Tom,Lily,