while循环
while在条件满足时循环
可能一次都不循环
要避免死循环
for循环
使用计数器进行循环
for (int i=0; i<100; i++) {
//
}
在循环内定义计数器变量
不要在循环体内修改计数器
for...each循环
for (int n : ns) {
//
}
for...each循环可以遍历数组
for...each循环不能指定遍历顺序
for...each循环无法获取计数器
public class ForCir {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ns = {4, 67, 44, 89, 56, 30};
for (int i = 0; i < ns.length; i++) {
System.out.println(ns[i]);
}
// for each
for(int n: ns) {
System.out.println(n); // 类似 python 中的 for n in ns: print(n)
}
}
}