2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-10-16 16:34:38 +02:00
An infinite loop is one that will never end while the program is running, i.e., you have to kill the program to get out of the loop. Whether it is by meeting the loop's end condition or via a ``++break++``, every loop should have an end condition.
2021-09-16 16:49:23 +03:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-09-16 16:49:23 +03:00
2022-02-04 17:28:24 +01:00
[source,kotlin]
2021-09-16 16:49:23 +03:00
----
var j: Int
while (true) { // Noncompliant; end condition omitted
j++
}
var k: Int
val b = true
while (b) { // Noncompliant; b never written to in loop
k++
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-09-16 16:49:23 +03:00
2022-02-04 17:28:24 +01:00
[source,kotlin]
2021-09-16 16:49:23 +03:00
----
var j: Int = 0
while (true) { // reachable end condition added
j++
if (j == Int.MIN_VALUE) { // true at Integer.MAX_VALUE +1
break
}
}
var k: Int = 0
var b = true
while (b) {
k++
b = k < Int.MAX_VALUE
}
----
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-09-16 16:49:23 +03:00
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-09-16 16:49:23 +03:00
endif::env-github,rspecator-view[]