CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Exit a for loop ??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Andreas



Joined: 25 Oct 2004
Posts: 136

View user's profile Send private message

Exit a for loop ??
PostPosted: Wed Aug 11, 2010 8:22 am     Reply with quote

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
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

Re: Exit a for loop ??
PostPosted: Wed Aug 11, 2010 8:26 am     Reply with quote

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?
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 11:18 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 2:26 pm     Reply with quote

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
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 3:42 pm     Reply with quote

Well, to be a little more specific, the break statement terminates the do, for, switch, or while statements while is within its encloses.

Humberto
deperkin



Joined: 04 Feb 2009
Posts: 83
Location: PA

View user's profile Send private message Send e-mail

complexity
PostPosted: Wed Aug 11, 2010 6:33 pm     Reply with quote

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;'
Andreas



Joined: 25 Oct 2004
Posts: 136

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 11:31 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group