difference between declaring an integer outside void setup

I've happened be introduce to Arduino in a magazine and have been hook ever since.
Currently learning Arduino programming through youtube channels.
However, I'm having a hard time as I've almost zero programming experience.

I would like post a basic question that I could not find on the net.
What is the difference between declaring the integer outside the void setup VS declaring the integer inside the void setup.
I was told that void setup only run once and I don't understand it.

For starter kit project 03 (love-o-meter)
the integer is declared outside...
but for servo the intger is declared inside.
When I switch it around, it doesn't run.

Can help explains what is going on?

#include <Servo.h>
Servo myServo;

int const potPin = A0;
int potVal;
int angle;

void setup() 
{
  myServo.attach(9);
  Serial.begin(9600);
}

void loop() 
{
  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);
  
  angle = map(potVal,0 ,1023, 0 ,179);
  Serial.print("angle: ");
  Serial.println(angle);
  
  myServo.write(angle);
  delay(15);
}

vs

#include <Servo.h>
Servo myServo;

int const potPin = A0;


void setup() 
{
int potVal;
int angle;
  myServo.attach(9);
  Serial.begin(9600);
}

void loop() 
{
  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);
  
  angle = map(potVal,0 ,1023, 0 ,179);
  Serial.print("angle: ");
  Serial.println(angle);
  
  myServo.write(angle);
  delay(15);
}

Hmm. I'd suggest googling "understanding variable scope and lifetime in C++".

Breifly: when you declare a variable inside a function, the variable only exists for so long as that function is running (lifetime). Also, when you declare a variable inside any code block {} , the variable is only visible to code in that block.

The benefit of this is that you can write code using its own internal variables, and those variables won't collide with other cariables even if they have the same names. eg:

void foo() {
  for(int i = 1: i <= 10; i++) {
    bar();
  }
}

void bar() {
  for(int i = 1: i <= 10; i++) {
    digitalWrite(5, HIGH); delay(100);
    digitalWrite(5, LOW); delay(100);
  }
}

You will get 100 blinks because the 'i' variable in foo and bar are completely different and nothing to do with one another.

Just addition, if the variable is declared outside, it is automatically global variable (the lifetime is same as the program lifetime) which can be visible in all functions. Global variable is initialized at the program start and it has 0 value if it is not initialized exactly at declaration.

I would suggest you start reading a book (or watch a video) on C programming before going into the specifics of arduino and acquire essential knowledge before going further.

there are tons of free resources on line to get started, for example this is the first hit on google for me. spend a bit of time practicing with the basics, you'll be very happy you did in the future.