-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathInfiniteLoop.qhelp
More file actions
49 lines (39 loc) · 1.23 KB
/
InfiniteLoop.qhelp
File metadata and controls
49 lines (39 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Loops can contain multiple exit conditions, either directly in the loop
condition or as guards around <code>break</code> or <code>return</code>
statements. If an exit condition cannot be satisfied, then the code is
misleading at best, and the loop might not terminate.
</p>
</overview>
<recommendation>
<p>
When writing a loop that is intended to terminate, make sure that all the
necessary exit conditions can be satisfied and that loop termination is clear.
</p>
</recommendation>
<example>
<p>
The following example shows a potentially infinite loop, since the inner loop
condition is constantly true. Of course, the loop may or may not be infinite
depending on the behavior of <code>shouldBreak</code>, but if this was
intended as the only exit condition the loop should be rewritten to make this
clear.
</p>
<sample src="InfiniteLoopBad.java" />
<p>
To fix the loop the condition is corrected to check the right variable.
</p>
<sample src="InfiniteLoopGood.java" />
</example>
<references>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-14.html">Blocks and Statements</a>.
</li>
</references>
</qhelp>