Because you are not finding the Nearest Enemy again between the first two attack commands and the second two attack commands, then the second two attacks are still looking to attack the original enemy you found - you will need to find a new enemy after killing the first one.
You are doing a search at that instant for the nearest enemy. You are not saying that enemy will always mean to do a search. So, once the first enemy dies you need to look for a new enemy.
It worked in the loop because everything in a loop is repeated over and over.