When the switch is on, the led is supposed to be on and when the switch is off,the led is supposed to turn off.
It complies and uploads correctly, and the built-in LED blinks, indicating that the program was sent to the Arduino. But the LED on pin 12 doesn't seem to be following what the switch does at all. It's just blinking randomly. Could anyone please explain what is going on?
You more than likely have a floating input. If the switch is open, the wire to the Arduino floats and picks up noise from the environment; this translates to random HIGH or LOW.
One good troubleshooting trick is to send variable-values or little messages to the serial monitor so you can "see" what your hardware & software is actually doing. For example, you can send a message that says "button pressed" and another message that says "turning-on LED", etc. The [u]Analog Read Serial Example[/u] shows you how to send messages to the serial monitor.
I almost always add these kind of messages while I'm developing the code. I don't wait until I'm troubleshooting. If you want to, or if you need to make your loop run faster, you can "comment-out" those serial transmissions when your code is done. Or there is a way to make a "compiler switch" so you can fairly-easily compile your normal code or the "debug version", but I've never done that.
It can happen if you have a power supply glitch that resets the processor or something like that, but It's usually a software or hardware error and the processor is following the instructions exactly as written, even if you're not getting the result you expected.