How to avoid loops in switches