|
| 1 | +package beginner; |
| 2 | + |
| 3 | + |
| 4 | +import com.sandwich.koan.Koan; |
| 5 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 6 | +import static com.sandwich.util.Assert.assertEquals; |
| 7 | + |
| 8 | + |
| 9 | +public class AboutLoops { |
| 10 | + |
| 11 | + @Koan |
| 12 | + public void basicForLoop() { |
| 13 | + String s = ""; |
| 14 | + for(int i = 0; i < 5; i++) { |
| 15 | + s += i + " "; |
| 16 | + } |
| 17 | + assertEquals(s, __); |
| 18 | + } |
| 19 | + |
| 20 | + @Koan |
| 21 | + public void basicForLoopWithTwoVariables() { |
| 22 | + String s = ""; |
| 23 | + for(int i = 0, j = 10; i < 5 && j > 5; i++, j--) { |
| 24 | + s += i + " " + j + " "; |
| 25 | + } |
| 26 | + assertEquals(s, __); |
| 27 | + } |
| 28 | + |
| 29 | + @Koan |
| 30 | + public void extendedForLoop() { |
| 31 | + int[] is = {1,2,3,4}; |
| 32 | + String s = "-"; |
| 33 | + for(int j : is) { |
| 34 | + s += "." + j; |
| 35 | + } |
| 36 | + assertEquals(s, __); |
| 37 | + } |
| 38 | + |
| 39 | + @Koan |
| 40 | + public void whileLoop() { |
| 41 | + int result = 0; |
| 42 | + while(result < 3) { |
| 43 | + result++; |
| 44 | + } |
| 45 | + assertEquals(result, __); |
| 46 | + } |
| 47 | + |
| 48 | + @Koan |
| 49 | + public void doLoop() { |
| 50 | + int result = 0; |
| 51 | + do { |
| 52 | + result++; |
| 53 | + } while(false); |
| 54 | + assertEquals(result, __); |
| 55 | + } |
| 56 | + |
| 57 | + @Koan |
| 58 | + public void extendedForLoopBreak() { |
| 59 | + String[] sa = {"Dog", "Cat", "Tiger" }; |
| 60 | + int count = 0; |
| 61 | + for(String current : sa) { |
| 62 | + if("Cat".equals(current)) { |
| 63 | + break; |
| 64 | + } |
| 65 | + count++; |
| 66 | + } |
| 67 | + assertEquals(count, __); |
| 68 | + } |
| 69 | + |
| 70 | + @Koan |
| 71 | + public void extendedForLoopContinue() { |
| 72 | + String[] sa = {"Dog", "Cat", "Tiger" }; |
| 73 | + int count = 0; |
| 74 | + for(String current : sa) { |
| 75 | + if("Dog".equals(current)) { |
| 76 | + continue; |
| 77 | + } else { |
| 78 | + count++; |
| 79 | + } |
| 80 | + } |
| 81 | + assertEquals(count, __); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + @Koan |
| 86 | + public void forLoopContinueLabel() { |
| 87 | + int count = 0; |
| 88 | + outerLabel: |
| 89 | + for(int i = 0; i < 5; i++) { |
| 90 | + for(int j = 0; j < 5; j++) |
| 91 | + { |
| 92 | + count++; |
| 93 | + if(count > 2) { |
| 94 | + continue outerLabel; |
| 95 | + } |
| 96 | + } |
| 97 | + count += 10; |
| 98 | + } |
| 99 | + // What does continue with a label mean? |
| 100 | + // What gets executed? Where does the program flow continue? |
| 101 | + assertEquals(count, __); |
| 102 | + } |
| 103 | + |
| 104 | + @Koan |
| 105 | + public void forLoopBreakLabel() { |
| 106 | + int count = 0; |
| 107 | + outerLabel: |
| 108 | + for(int i = 0; i < 5; i++) { |
| 109 | + for(int j = 0; j < 5; j++) |
| 110 | + { |
| 111 | + count++; |
| 112 | + if(count > 2) { |
| 113 | + break outerLabel; |
| 114 | + } |
| 115 | + } |
| 116 | + count += 10; |
| 117 | + } |
| 118 | + // What does break with a label mean? |
| 119 | + // What gets executed? Where does the program flow continue? |
| 120 | + assertEquals(count, __); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments