forked from RichardGong/PlayWithCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.play
62 lines (54 loc) · 973 Bytes
/
loop.play
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
测试几种循环语句。
包括对break的支持(只跳出一层循环)。
*/
//测试while循环
println("while loop:");
int i = 0;
while(i < 10){
i = i+1;
println("i="+i);
}
//测试for循环
println();
println("for loop:");
int a = 0;
for(int i = 0; i<10; i++){
a = a + i;
println("i="+i + ", a="+a);
}
//测试带break的while循环
println();
println("while loop, breaks when i > 5:");
i = 0;
while(i < 10){
i = i+1;
println("i="+i);
if (i > 5)
break;
}
//测试带break的for循环
println();
println("for loop, breaks when i > 5:");
a = 0;
for(int i = 0; i<10; i++){
a = a + i;
println("i="+i + ", a="+a);
if (i > 5)
break;
}
//测试嵌套循环,和break
println();
println("for loop within while loop:");
i = 0;
while (i<5){
i = i+1;
for (int j = 0; j< 5; j++){
println("i="+i + ", j="+j);
if (j>=2)
break;
}
if (i >= 3){
break;
}
}