Indeed, in the first example the special cases should really be moved out of the loop too, so you have the code looking like this:
- code for left edge
- code for body
- code for right edge
Now, both you and the machine don't have to keep thinking about the edge cases every time through the loop body. It's a win-win situation.