001package beginner;
002
003
004import com.sandwich.koan.Koan;
005
006import static com.sandwich.koan.constant.KoanConstants.__;
007import static com.sandwich.util.Assert.assertEquals;
008
009
010public class AboutLoops {
011
012    @Koan
013    public void basicForLoop1() {
014        String s = "";
015        for (int i = 0; i < 5; i++) {
016            s += i + " ";
017        }
018        assertEquals(s, __);
019    }
020
021    @Koan
022    public void basicForLoop2() {
023        String s = "";
024        for (int i = -5; i < 1; i++) {
025            s += i + " ";
026        }
027        assertEquals(s, __);
028    }
029
030    @Koan
031    public void basicForLoop3() {
032        String s = "";
033        for (int i = 5; i > 0; i--) {
034            s += i + " ";
035        }
036        assertEquals(s, __);
037    }
038
039    @Koan
040    public void basicForLoop4() {
041        String s = "";
042        for (int i = 0; i < 11; i += 2) {
043            s += i + " ";
044        }
045        assertEquals(s, __);
046    }
047
048    @Koan
049    public void basicForLoop5() {
050        String s = "";
051        for (int i = 1; i <= 16; i *= 2) {
052            s += i + " ";
053        }
054        assertEquals(s, __);
055    }
056
057    @Koan
058    public void basicForLoopWithTwoVariables1() {
059        String s = "";
060        for (int i = 0, j = 10; i < 5 && j > 5; i++, j--) {
061            s += i + " " + j + " ";
062        }
063        assertEquals(s, __);
064    }
065
066    @Koan
067    public void nestedLoops() {
068        String s = "";
069        for (int i = 0; i < 3; i++) {
070            for (int j = 0; j < 3; j++) {
071                s += "(" + i + ", " + j + ") ";
072            }
073            s += " - ";
074        }
075        assertEquals(s, __);
076    }
077
078    @Koan
079    public void extendedForLoop() {
080        int[] is = {1, 2, 3, 4};
081        String s = "";
082        for (int j : is) {
083            s += j + " ";
084        }
085        assertEquals(s, __);
086    }
087
088    @Koan
089    public void whileLoop() {
090        int result = 0;
091        while (result < 3) {
092            result++;
093        }
094        assertEquals(result, __);
095    }
096
097    @Koan
098    public void doLoop() {
099        int result = 0;
100        do {
101            result++;
102        } while (false);
103        assertEquals(result, __);
104    }
105
106    @Koan
107    public void extendedForLoopBreak() {
108        String[] sa = {"Dog", "Cat", "Tiger"};
109        int count = 0;
110        for (String current : sa) {
111            if ("Cat".equals(current)) {
112                break;
113            }
114            count++;
115        }
116        assertEquals(count, __);
117    }
118
119    @Koan
120    public void extendedForLoopContinue() {
121        String[] sa = {"Dog", "Cat", "Tiger"};
122        int count = 0;
123        for (String current : sa) {
124            if ("Dog".equals(current)) {
125                continue;
126            } else {
127                count++;
128            }
129        }
130        assertEquals(count, __);
131    }
132
133    @Koan
134    public void forLoopContinueLabel() {
135        int count = 0;
136        outerLabel:
137        for (int i = 0; i < 6; i++) {
138            for (int j = 0; j < 6; j++) {
139                count++;
140                if (count > 2) {
141                    continue outerLabel;
142                }
143            }
144            count += 10;
145        }
146        // What does continue with a label mean?
147        // What gets executed? Where does the program flow continue?
148        assertEquals(count, __);
149    }
150
151    @Koan
152    public void forLoopBreakLabel() {
153        int count = 0;
154        outerLabel:
155        for (int i = 0; i < 4; i++) {
156            for (int j = 0; j < 4; j++) {
157                count++;
158                if (count > 2) {
159                    break outerLabel;
160                }
161            }
162            count += 10;
163        }
164        // What does break with a label mean?
165        // What gets executed? Where does the program flow continue?
166        assertEquals(count, __);
167    }
168}