'If' statement not working in my code

I am a beginner to Arduino so I don't really know if this is the correct place to say this, but it seems that my 'if' statement isn't working

If I use this as my code, it automatically set pin 8 to high without me pressing the button:
void setup()
{
pinMode(13,INPUT);
pinMode(8,OUTPUT);
}

void loop()
{
int button = digitalRead(13);
if(button == 1)
{
digitalWrite(8,HIGH);
}
}

If I use this as my code, it automatically sets it to off anyway:
void setup()
{
pinMode(13,INPUT);
pinMode(8,OUTPUT);
}

void loop()
{
int button = digitalRead(13);
if(button == 0)
{
digitalWrite(8,HIGH);
}
}

It seems that my code is thinking that my button is always pressed.

Wow - an Arduino newbie having read enough to use active LOW logic like they should! That's good!

You need a pullup for the button pin. If you do pinMode(13,INPUT_PULLUP) it should work - assuming it is wired correctly. Otherwise, the input is "floating" and will transition randomly when the button isn't pressed as it picks up noise from the environment. One can also use an external resistor instead, but this is rarely necessary, since there are those internal pullups. If you ever find yourself needing an external pullup or pulldown resistor, 4.7k-10k is a good "default" value.

Is it wired correctly? Button should connect to ground when pressed. With those el-cheapo 4-pin buttons that are common in starter kits, there are two pairs of pins that are each connected internally - so if you use the wrong two pins, they'll always be connected.

Finally - pin13 is the pin used for the builtin LED

On an Uno/Leonardo (and some of the other boards - I don't use the non-avr boards, so I don't know off hand which), pin 13 is "buffered" to drive the LED - there's a separate component (specifically, an op amp wired as a comparator) which watches the pin without loading it down, so the LED doesn't put any load on pin 13. So it should be find to use them for the button on those boards.

However, if you're using a nano, pro mini, or micro - smaller, cheaper boards - these do not have that, and have the LED connected directly to pin13 through a resistor. This puts a load on the pin such that the internal pullup - or even a typical external pullup - won't be able to pull the pin high. On those boards, use a different pin for the button (an unusually strong external pullup would also work)

Here try this code.

int button = 13;
int led = 8; // set the name and pin you want

void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT)
}

void loop() {
int buttonState = digitalRead(button);
if (buttonState == 0) {
digitalWrite(led, HIGH)// change led to what name you put if you changed it
}
}

pinMode(8, OUTPUT)Oops.

If you didn’t compile or test the code, it is considered polite to point it out.

Please remember to use code tags when posting code