Pause and resume for loop in C++

Status
Not open for further replies.

AAOAA

Junior Member level 2
Joined
Oct 31, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
150
Hello,

I would like to know is there a way to to return back to the last iteration of the for loop, after the break and continue from there, for ex.:

Code:
void loop()
 {for (n ; n<10; n++)
  if (n=5) {break;}}
                         //code lines
                         //code lines 
                         //code lines
 loop();               //should start from 6 ...

Thanks.
 
Last edited by a moderator:

I assume loop() is a function - define your for() control variable as static and it will remember the last value
Code:
void loop()
 {
      static int n=0;
      for (; n<10; n++)
         {
         if (n==5) { i++; return; }
                         //code lines
                         //code lines 
                         //code lines
          }
}
on first call to loop() n would be 0 - next time loop() is called n will be 6
what happens when n becomes 10? every time the function is called n will be 10 and the for() not executed
not sure why you would wish to do this???
 
Reactions: AAOAA

    AAOAA

    Points: 2
    Helpful Answer Positive Rating
You probably has not figured out a correct way to solve your problem and ask strange questions...
The is also the setjmp.h with its functions/structures that save and restore execution states.
 

Thanks horace1,

This is working for me, actually I am using this as a part of my code for a microcontroller, the for loop variable is used as an index for an array in a restrict timing regulation, anyway when the value reaches 10 I can reset it then in an other part.

Code:
if (cnt!=0 && Error<0) 
{
cnt--; pattern >>= 1; 
for ( n = 0; n <MASK_SIZE; n++) 
{
dc_mask = masks [n]; 
break;
}
}
 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…