Loops are used to execute a code block many times.
A loop can be used to print all numbers in certain range. Like from 1 to 10
Output:
At line 4 the condition of the loop is set. Since num is less than 10 the loop executes.
line 4
num
In order to print the numbers to 10, num increases by 1 at line 6.
line 6
The loop ends when num reaches value of 11.
Infinite Loop has a condition that's always true.
It can be created due to development mistake or used in advanced algorithm.
Here are examples:
Since num is never changed, the condition $num < 5 is always true and the loop executes infinitely (or until the program takes more ram than it is allowed).
$num < 5
In order to end the loop num needs to be incremented (like $num++) inside the loop's body.
$num++
Since the condition at line 4 is always true, the loop keeps on repeating.
true