Just test the end case inside the loop, rather than trying to be clever and getting it wrong.
Attach a file by drag & drop or click to upload