void loop () decleration problem

Friends, I am working on a prjecet bu there is a problem. The problem seems ugly to me

1- I have seen that when ''void loop()'' function is executed fully firstly and starts second exexution every declared variable data at the end of the first execution is lost when it starts next execution of loop. Is this the case or am I wrong?

My case: I am using a variable lets say a=k*5. k is read value from sensors. Lets say at the end of the first execution of ''void loop()'' the variable is 231. if I write printout function at each loop before the '' a=k*5'' it always prints 0. Note I have defined the variable in ''void loop()'' function. How can I define this variable in a way that I can use it for next loop.

2- What is the difference between defining a variable or constant in the ''void loop()'' and out of the loop?

Thanks in advance

The loop() function is called over and over again. Every time the functions ends, all variables are lost. That is: the local variables on the stack.

You can create a global variable, outside the loop() function. That global variable is in ram and has it's own location, and stays there.

Another way is to use the word 'static'. That forces a local variable to be created in ram just as an global variable, and not on the stack. The variable is however only usable in the loop() function, the scope is still within the loop() function.

int supercars;   // global variable.

void loop()
{
  int berries;
  static int clouds;

  // supercars is global, it will keep its value.
  supercars++;

  // clouds is static, it will keep its value;
  clouds += 3;

  // berries is a variable on the stack, it will loose its value.
  berries = 5;
}

A normal variable is only temporary. It is declared on the stack and unuseable after the functions ends. Did you know that you can create variables inside a for-loop or while-loop ? In that case the variable is 'extra' temporary so to speak. The scope of the variable is only within that loop.

Using global variables can make a bad sketch. Those variables can be changed in any function. A good sketch has a well-balanced use between global variables and normal variables and maybe a few 'static' variables. Every variable according to its use.