Saving constant variables in the code while looping

Hi

So I got in my mind a question that I can't imagine how that can be done,
I want to have a switch and 2 variables x and y the x will be a variable which will always loop
from 0 to 100 in the loop and the y variable will be like a saving but constant variable.

I got the x in a for loop to loop from 0 to 100 so on and the y constant variable waiting,
if I turn the switch ON the "y" variable will take the number of "x" for example, if I turn the switch ON
while the x = 54 the y will take 54 (and the x will keep looping) and y will be = 54 until the switch will turn
OFF but till then the y will be and stay constant at y =54.

if (digitalRead(switch) == LOW) y = x;

yea but I don't need my "y" to be always what "x" is, i want the "y" to take only once the value of x
not always.

If I switch ON the "y" will take only once the value of "x" and stay with that value and not change it.

Initialize y with an invalid number; e.g. -1. Test for it before assigning a value.

for(int x = 0; x< 100;x++)
{
  if (y == -1 && digitalRead(button) == LOW)
    y = x;
  if (digitalRead(button) == HIGH)
    y = -1;
}

Bouncing of the switch not taken into account.

Domino60:
yea but I don't need my "y" to be always what "x" is, i want the "y" to take only once the value of x
not always.

Before entering the loop, make y a number which will never occur inside the loop, for example -1. And within the loop only change it to the x value if the y value is -1.

int y = - 1 ; // -1 means not set

while (whatever) {
    if (y != -1) {
         // y not set yet, so set it when required
         ...
    }
}

I'm guessing that there is a value of y which you can use, I guessed -1. So you can see that once it is changed from -1 to some "valid" value it will never get changed again.

for(int x = 0; x< 100;x++)
{
  if (y == -1 && digitalRead(button) == LOW)
    y = x;
  if (digitalRead(button) == HIGH)
    y = -1;
}

If I turn ON the switch and keep it ON the y will be always what x is while looping, what I need is
when i turn ON the switch and keep it ON the y to take only once the value of x but not always.

With your example I got this Switch: ON
x=1
y=1
x=2
y=2
...
x=100
y=100

But I need this:

Switch: OFF
x=1
x=2
x=3
...
x=10
Switch: ON
x=11
y=11
x=12
y=11
x=13
y=11
...
x=100
y=11

Perhaps you need a Boolean flag say "ySet" which you initialise false, then set true when you do the y=x thing. Only do the y=x if ySet is false. Unset it later id it's appropriate.

PaulS puts it this way:

bool beenThereDoneThat = false;

if(!beenThereDoneThat)
{
    GoThereDoThat();
    beenThereDoneThat = true;
}

It depends on the scope of y. The code assumes it's global or static within the function where you use it (e.g. loop()).

void loop()
{
static int y = -1;
  for(int x = 0; x< 100;x++)
  {
    if (y == -1 && digitalRead(button) == LOW)
      y = x;
    if (digitalRead(button) == HIGH)
      y = -1;
  }
}

Looked at the StateChangeDetection example?