Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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:

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
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???
 
  • Like
Reactions: AAOAA

    AAOAA

    Points: 2
    Helpful Answer Positive Rating

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
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.
 

AAOAA

Junior Member level 2
Joined
Oct 31, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
150
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top