How do you cycle through animations through the use of c# code? Unity 3D

My zombie character will attack and walk towards the player when the player is close to it. However when the player is out of the zombie’s range it will keep attacking instead of doing an idle animation.