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

C-question

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







C-question
PostPosted: Thu Jul 14, 2005 11:54 am     Reply with quote

I've got a general C question. Say I call the function my_fxn(), and i'm stuck in the while-loop. The normal way to get out of the while loop is if tmr0_value gets to >= 2000. However, if 'distance' ever goes more than 100, i want to break out of the while loop prematurely.

Is it correct that i use 'break;'? (The only time i've ever used 'break' was after each case in a a switch-case statement.) Or should I set a flag, and test it in the while loop?

I know this is a naive question, but i admit i don't know all my C. Embarassed

Code:

void my_fxn(void)
{
   while (tmr0_value < 2000)
   {
       ...
       if (distance > 100)
       {
           break;
       }
   }
   
   ... next instruction after while loop.
   ...
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Thu Jul 14, 2005 12:33 pm     Reply with quote

The proper way in my book
Humberto



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

View user's profile Send private message

PostPosted: Fri Jul 15, 2005 9:25 am     Reply with quote

Hi Mike,

Code:


int16 tmr0_value;
int8  distance;

void my_fxn(void)
{
  do
     {
       ...
       ...
     }while ((tmr0_value < 2000) && (distance <= 100));

   ... next instruction after while loop.
   ...
}


Best wishes,

Humberto
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Fri Jul 15, 2005 10:00 am     Reply with quote

… and if you want to break out of a bunch of nested loop, the following trick can be useful

Code:

while (a < b)
{
   while (c > d)
   {
      if (e == f)
      {
         goto afterloop;
      }
}
}
afterloop:
// more code …
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