Although there may be certain special cases in which it can be determined that the current iteration is the last, in the general case this is not possible. Trivial example:
while (rand() < 0.99) { print "Hasn't ended yet\n"; }
Since it is impossible to predict what the next random number will be, it is obvious that it is impossible to determine whether any iteration will be the last iteration.
source share