View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
C-question |
Posted: Thu Jul 14, 2005 11:54 am |
|
|
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.
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
|
|
Posted: Thu Jul 14, 2005 12:33 pm |
|
|
The proper way in my book |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Jul 15, 2005 9:25 am |
|
|
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
|
|
Posted: Fri Jul 15, 2005 10:00 am |
|
|
� 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 �
|
|
|
|
|