View Javadoc
1   package integrationTests.loops;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   public class WhileStatements {
7   
8       /** The logger. */
9       private static final Logger logger = LoggerFactory.getLogger(WhileStatements.class);
10  
11      void whileBlockInSeparateLines() {
12          int i = 0;
13  
14          while (i < 5) {
15              i++;
16          }
17      }
18  
19      void whileBlockInSingleLine(int i) {
20          // @formatter:off
21          while (i < 2) { i++; }
22          // @formatter:on
23      }
24  
25      int whileWithContinue(int i) {
26          while (i < 2) {
27              if (i == 1) {
28                  i = 3;
29                  continue;
30              }
31  
32              i++;
33          }
34  
35          return i;
36      }
37  
38      int whileWithBreak(int i) {
39          while (i < 2) {
40              if (i == 1) {
41                  break;
42              }
43  
44              i++;
45          }
46  
47          return i;
48      }
49  
50      void nestedWhile(int i, int j) {
51          while (j < 2) {
52              while (i < j) {
53                  i++;
54              }
55  
56              j++;
57          }
58      }
59  
60      void doWhileInSeparateLines() {
61          int i = 0;
62  
63          do {
64              i++;
65          } while (i < 3);
66      }
67  
68      void bothKindsOfWhileCombined(int i, int j) {
69          while (true) {
70              do {
71                  i++;
72              }
73              // @formatter:off
74              while (i < j);
75              // @formatter:on
76  
77              j++;
78  
79              if (j >= 2) {
80                  return;
81              }
82          }
83      }
84  
85      void whileTrueEndingWithAnIf(int i) {
86          while (true) {
87              i++;
88  
89              if (i >= 2) {
90                  return;
91              }
92          }
93      }
94  
95      void whileTrueStartingWithAnIf(int i) {
96          while (true) {
97              if (i >= 2) {
98                  return;
99              }
100 
101             i++;
102         }
103     }
104 
105     void whileTrueWithoutExitCondition() {
106         while (true) {
107             doSomething();
108         }
109     }
110 
111     void whileTrueContainingTryFinally() {
112         while (true) {
113             try {
114                 doSomething();
115             } finally {
116                 doNothing();
117             }
118         }
119     }
120 
121     private static void doSomething() {
122         throw new IllegalStateException();
123     }
124 
125     private static void doNothing() {
126     }
127 
128     int whileWithIfElse(int i) {
129         while (i <= 2) {
130             if (i % 2 == 0) {
131                 logger.info("even");
132             } else {
133                 logger.info("odd");
134             }
135 
136             i++;
137         }
138 
139         return i;
140     }
141 }