Spinning back and forth

Hi!

I have a square in my game which i want to spin back and forth (from z: 20 to z: -20).
So i tried to make an IEnumerator

IEnumerator spin(){
        Vector3 spin;
        while (true) {
            for (int i = 0; i < 40; i++) {
                spin = gameObject.transform.eulerAngles;
                spin.z -= 1;
                gameObject.transform.eulerAngles = spin;
                yield return new WaitForEndOfFrame ();
            }

            for (int i = 0; i < 40; i++) {
                spin = gameObject.transform.eulerAngles;
                spin.z += 1;
                gameObject.transform.eulerAngles = spin;
                yield return new WaitForEndOfFrame ();
            }
        }
    }

But the square doesn’t behave the way i want it to. It spins one way, then stops and spins the same way again …
Anybody know what I’m doing wrong?

Thanks

I put your code to test and it seemed to work just fine. It rotate my sprite from 0 degree to -40. Then from -40 back to 0. If you specifically wants 20 to -20, guess it just need to start at the correct eulerAngles.