Skip to content

Commit e2d926c

Browse files
committed
more loop koans
1 parent 609e479 commit e2d926c

1 file changed

Lines changed: 56 additions & 9 deletions

File tree

‎koans/src/beginner/AboutLoops.java‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class AboutLoops {
1111

1212
@Koan
13-
public void basicForLoop() {
13+
public void basicForLoop1() {
1414
String s = "";
1515
for (int i = 0; i < 5; i++) {
1616
s += i + " ";
@@ -19,20 +19,68 @@ public void basicForLoop() {
1919
}
2020

2121
@Koan
22-
public void basicForLoopWithTwoVariables() {
22+
public void basicForLoop2() {
23+
String s = "";
24+
for (int i = -5; i < 1; i++) {
25+
s += i + " ";
26+
}
27+
assertEquals(s, __);
28+
}
29+
30+
@Koan
31+
public void basicForLoop3() {
32+
String s = "";
33+
for (int i = 5; i > 0; i--) {
34+
s += i + " ";
35+
}
36+
assertEquals(s, __);
37+
}
38+
39+
@Koan
40+
public void basicForLoop4() {
41+
String s = "";
42+
for (int i = 0; i < 11; i += 2) {
43+
s += i + " ";
44+
}
45+
assertEquals(s, __);
46+
}
47+
48+
@Koan
49+
public void basicForLoop5() {
50+
String s = "";
51+
for (int i = 1; i <= 16; i *= 2) {
52+
s += i + " ";
53+
}
54+
assertEquals(s, __);
55+
}
56+
57+
@Koan
58+
public void basicForLoopWithTwoVariables1() {
2359
String s = "";
2460
for (int i = 0, j = 10; i < 5 && j > 5; i++, j--) {
2561
s += i + " " + j + " ";
2662
}
2763
assertEquals(s, __);
2864
}
2965

66+
@Koan
67+
public void nestedLoops() {
68+
String s = "";
69+
for (int i = 0; i < 3; i++) {
70+
for (int j = 0; j < 3; j++) {
71+
s += "(" + i + ", " + j + ") ";
72+
}
73+
s += " - ";
74+
}
75+
assertEquals(s, __);
76+
}
77+
3078
@Koan
3179
public void extendedForLoop() {
3280
int[] is = {1, 2, 3, 4};
33-
String s = "-";
81+
String s = "";
3482
for (int j : is) {
35-
s += "." + j;
83+
s += j + " ";
3684
}
3785
assertEquals(s, __);
3886
}
@@ -86,8 +134,8 @@ public void extendedForLoopContinue() {
86134
public void forLoopContinueLabel() {
87135
int count = 0;
88136
outerLabel:
89-
for (int i = 0; i < 5; i++) {
90-
for (int j = 0; j < 5; j++) {
137+
for (int i = 0; i < 6; i++) {
138+
for (int j = 0; j < 6; j++) {
91139
count++;
92140
if (count > 2) {
93141
continue outerLabel;
@@ -104,8 +152,8 @@ public void forLoopContinueLabel() {
104152
public void forLoopBreakLabel() {
105153
int count = 0;
106154
outerLabel:
107-
for (int i = 0; i < 5; i++) {
108-
for (int j = 0; j < 5; j++) {
155+
for (int i = 0; i < 4; i++) {
156+
for (int j = 0; j < 4; j++) {
109157
count++;
110158
if (count > 2) {
111159
break outerLabel;
@@ -117,5 +165,4 @@ public void forLoopBreakLabel() {
117165
// What gets executed? Where does the program flow continue?
118166
assertEquals(count, __);
119167
}
120-
121168
}

0 commit comments

Comments
 (0)