How to prevent a Samsung Galaxy S4 from auto-restarting?
Try to get into the recovery. You can access it by pressing Volume UP + Power Button. When your screen turns on, release the Power Button. 
If you can get into the recovery without constant restarting problems, you can make a "factory reset". This will delete all your data and settings but might solve.

But as a side note: Try to provide more information. When did it start turning itself on/off? What apps did you install before? What is your android version and is your device rooted or not?