1.1 增强型for循环
public class HelloWorld {
public static void main(String[] args) {
int values [] = new int[]{18,62,68,82,65,9};
//常规遍历
for (int i = 0; i < values.length; i++) {
int each = values[i];
System.out.println(each);
}
//增强型for循环遍历
**for (int each : values) {
System.out.println(each);
}**
}
}
与普通的for循环不同,foreach不需要指定数组的长度,也无须通过数组的索引(下标)来访问数组元素,同时没有循环条件,而是要遍历整个数组或者集合。
1.2 为什么Java中没有goto语句
注意一点:java把goto和const列为关键字
在结构化语言程序设计过程中,乱用goto可能会导致程序流程的混乱,严重影响程序的可读性和可维护性
java中可以通过使用break-label和continue-label,在多层循环中跳转