Loops bring great power but also great responsability. The biggest danger when using loops is writing an infinite loop.
An infinite loop is a loop that never terminates. For example, if we forget to increment i in our previous example:
var i = 1
while(i < 5) {
println(i)
// i++; => forgot this
}
This program will never terminate as i will always have value 1 and thus the condition i < 5 will always be satisfied.
The program would basically print 1 forever never exiting the while loop.
Pro Tip:
As programmers, we want to make sure we never write infinite loops as they make our program run forever and completely unusable.
Assignment
Follow the Coding Tutorial and practice with infinite while loops!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. Premium is unlimited.