As suggested you need to set up a resistor as a 'pull down' resistor to stop the floating input when the switch is off. In that mode it is actually not off (0) but the Arduino pin is actually not connected at all. The problem is not a software one but related to your hardware configuration.