View previous topic :: View next topic |
Author |
Message |
Andreas
Joined: 25 Oct 2004 Posts: 136
|
Exit a for loop ?? |
Posted: Wed Aug 11, 2010 8:22 am |
|
|
Hi friends,
A new question comes up during coding today:
Is it possible to exit a for loop ?
I just want to run an infinite loop with a check of a Value and if this value gets true the for loop should be exit.
Is this possible in CCS C ?
best regards
Andreas |
|
![](templates/subSilver/images/spacer.gif) |
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
Re: Exit a for loop ?? |
Posted: Wed Aug 11, 2010 8:26 am |
|
|
Andreas wrote: | Hi friends,
A new question comes up during coding today:
Is it possible to exit a for loop ?
I just want to run an infinit loop with a check of a Value and if this value gets true the for loop should be exit.
Is this possible in CCS C ?
best regards
Andreas |
Have you tried "break"? This is a very, very basic C question. Maybe you should get a book on C from the bookstore? |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Aug 11, 2010 11:18 am |
|
|
A while() loop will work for that. You can have it evaluate whatever variable you want and when it becomes true, the while() loop exits.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19388
|
|
Posted: Wed Aug 11, 2010 2:26 pm |
|
|
For both a while loop, and a for loop, the same answer applies. Break.
So:
Code: |
for(;;) {
//Permanent for loop
if (condition) break;
}
//You get here when 'condition' goes true.
while (TRUE){
if (condition) break;
}
//Same comment
do {
if (condition) break;
} while (TRUE);
//and again
|
The important thing is that in each loop, it is perfectly possible to have an 'exit' condition in the loop criteria, but this will only be tested at the end of the loop (or start), but with 'break', you can exit the loop from a point well inside.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Aug 11, 2010 3:42 pm |
|
|
Well, to be a little more specific, the break statement terminates the do, for, switch, or while statements while is within its encloses.
Humberto |
|
![](templates/subSilver/images/spacer.gif) |
deperkin
Joined: 04 Feb 2009 Posts: 83 Location: PA
|
complexity |
Posted: Wed Aug 11, 2010 6:33 pm |
|
|
using a break; is far too simple...
why not create a section of code to write to an external eeprom and use a WHILE to check a specific memory location for any change...
maybe add a secondary uC just to monitor changes on this external eeprom location and use i2c...
or you could just use 'break;' |
|
![](templates/subSilver/images/spacer.gif) |
Andreas
Joined: 25 Oct 2004 Posts: 136
|
|
Posted: Wed Aug 11, 2010 11:31 pm |
|
|
Hi Friends,
Thanks for this clear answers..........
The reason why I wa asking was that in the CCS Manual the break possibility for the FOR loop was not mentioned.
But anyhow evrybody can learn........
Andreas |
|
![](templates/subSilver/images/spacer.gif) |
|