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