If you think of your loops as a structure similar to this (somewhat pseudo-code):
loop:
if condition then
... //execute body
goto loop
else
...
it might make a little bit more sense. A loop is essentially just an if
statement that is repeated until the condition is false
. And this is the important point. The loop checks its condition and sees that it's false
, thus executes the else
(just like a normal if/else
) and then the loop is done.
So notice that the else
only get's executed when the condition is checked. That means that if you exit the body of the loop in the middle of execution with for example a return
or a break
, since the condition is not checked again, the else
case won't be executed.
A continue
on the other hand stops the current execution and then jumps back to check the condition of the loop again, which is why the else
can be reached in this scenario.