Java 循环结构
while、do-while、for、for-each 循环与 break/continue。
while 循环
while (cond) tests before each pass and runs the body only when true. The body must move the condition toward false, or the loop never ends.
public class WhileDemo {
public static void main(String[] args) {
int x = 10;
while (x < 15) {
System.out.print("value of x : " + x + "\n");
x++;
}
}
}value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14do…while 循环
do { } while (cond); runs the body once before testing, so it always executes at least once. The trailing semicolon is required.
public class DoWhile {
public static void main(String[] args) {
int x = 10;
do {
System.out.print("value of x : " + x + "\n");
x++;
} while (x < 12);
int y = 100;
do {
System.out.println("runs once even though 100 < 12 is false");
} while (y < 12);
}
}value of x : 10
value of x : 11
runs once even though 100 < 12 is falsefor 循环
for (init; cond; update) puts all three parts on one line and suits counted loops. Any part may be omitted, but the semicolons stay.
public class ForDemo {
public static void main(String[] args) {
for (int x = 10; x < 15; x = x + 1) {
System.out.print("value of x : " + x + "\n");
}
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println("sum = " + sum);
}
}value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
sum = 15Java 增强 for 循环
for (Type v : arrayOrCollection) walks elements without indices and cannot go out of bounds. The trade-off is no index and no safe removal during iteration.
public class ForEach {
public static void main(String[] args) {
int[] nums = {10, 20, 30};
for (int n : nums) {
System.out.print(n + " ");
}
System.out.println();
String[] names = {"a", "b"};
for (String s : names) {
System.out.println(s.toUpperCase());
}
}
}10 20 30
A
Bbreak 与 continue
break leaves the whole loop; continue skips the rest of the current pass. Both affect only the innermost loop unless you use a label like outer:.
public class BreakContinue {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 3) break;
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
System.out.println();
}
}0 1 2
1 3 while 循环
while (条件) 每轮开始前判断,为真才执行循环体。循环体里必须有让条件趋向为假的语句,否则就是死循环。
public class WhileDemo {
public static void main(String[] args) {
int x = 10;
while (x < 15) {
System.out.print("value of x : " + x + "\n");
x++;
}
}
}value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14do…while 循环
do { } while (条件); 先执行一次再判断,因此循环体至少执行一次。注意结尾的分号不能省。
public class DoWhile {
public static void main(String[] args) {
int x = 10;
do {
System.out.print("value of x : " + x + "\n");
x++;
} while (x < 12);
int y = 100;
do {
System.out.println("runs once even though 100 < 12 is false");
} while (y < 12);
}
}value of x : 10
value of x : 11
runs once even though 100 < 12 is falsefor 循环
for (初始化; 条件; 更新) 把三件事写在一行,最适合次数确定的循环。三个部分都可省略,但分号必须保留。
public class ForDemo {
public static void main(String[] args) {
for (int x = 10; x < 15; x = x + 1) {
System.out.print("value of x : " + x + "\n");
}
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println("sum = " + sum);
}
}value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
sum = 15Java 增强 for 循环
for (类型 变量 : 数组或集合) 逐个取出元素,不用管下标,也不会越界。缺点是拿不到索引,也无法在遍历时安全删除元素。
public class ForEach {
public static void main(String[] args) {
int[] nums = {10, 20, 30};
for (int n : nums) {
System.out.print(n + " ");
}
System.out.println();
String[] names = {"a", "b"};
for (String s : names) {
System.out.println(s.toUpperCase());
}
}
}10 20 30
A
Bbreak 与 continue
break 立刻跳出整个循环,continue 跳过本轮剩余语句进入下一轮。二者默认只影响最内层循环,加标签(outer:)可以跳出外层。
public class BreakContinue {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 3) break;
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
System.out.println();
}
}0 1 2
1 3