Keeping variables between different for loops

so i have googled the crap out of this topic, but i havent found an example of what im trying to do, I basically have 3 if statements that trigger 3 different for loops which are basically countdown timers. the for loops all use the variable x which is set to a number and decremented to zero. but I want to add a 4th if statement/for loop that utilizes the last value that x was (pause/play button). so for instance initiate if statement one which does a for loop and decrements x starting from ten press the pause/play button when its at 5 then have the option to restart from the x=5 value or initiate another if statement all together.

There are two choices. 1) Make the variable global, so it is visible everywhere in the program, and retains its value. 2) Make the variable static, so it can be seen only in the function, but retains its value between function calls.

Unless you break out of the for loop it will run to completion. How are you breaking out of the for loop when the button is pressed ? Presumably you have some way in there to make the for loop run slowly but as you have not posted your code (HINT) we do not know.

If you have a variable that is properly scoped that you would like to use, you can leave the first term out of the for loop statement like this:

int x;

//  ... some code that puts a value in x

for( ; x>=0; x--) {
     // do your thing with x

// more code here, the value of x will now be 0 unless you broke out of the for loop at some point.

sorry yes i am breaking out of the for loop i guess i didnt make that part clear. i have a delay of 10 with a decrement of x = x - .001 to allow many opportunities to break so the button is super responsive. and ive noticed that no matter how i declare that variable it gets lost when i break. so im basically looking for a way to store it back to a variable that can be recalled. sorry i didnt post code its on my other computer i will put it up when i get home tonight. thanks for the ideas so far.

Please stop posting bits and pieces and post your whole program.

How and where is the x used in your for loop declared, for example ?

im basically looking for a way to store it back to a variable that can be recalled

If that is how you want to do it then why not just save the value of x to a global variable as the first statement of the for loop then copy it back to x as the starting value for the for loop when you want to restart it.

i have a delay of 10 with a decrement of x = x - .001

Delay takes an integer argument. Whatever you're doing with the 0.001 is being rounded.

Post your code or this discussion is fairly moot.