Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 30th August 2008, 04:03 AM   (permalink)
Default Using 'continue' in Hi-Tech C

Hi,
I'm now learning to use Hi-Tech C. Previously, I use C18 compiler.

In Hi-Tech C, I'm trying to poll the GODONE bit in ADC. In the example, they use

while (GODONE) continue;

Is there any difference if I use while (GODONE); instead (without 'continue')? When I use the second one, it is able to build well without any error.

Please advice. Thanks.
MrNobody is offline  
Old 30th August 2008, 05:51 AM   (permalink)
Default

Quote:
Originally Posted by MrNobody View Post
In Hi-Tech C, I'm trying to poll the GODONE bit in ADC. In the example, they use

while (GODONE) continue;

Is there any difference if I use while (GODONE); instead (without 'continue')? When I use the second one, it is able to build well without any error.

Please advice. Thanks.
Code:
while(GODONE);
is the way it's done. Standard practice. You can also do
Code:
while(GODONE){}
if you want.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 30th August 2008 at 05:54 AM.
futz is online now  
Old 30th August 2008, 06:00 AM   (permalink)
Default

Futz is right.

Continue works within loops where its effect is to force an immediate jump to the loop control statement. Lots of info on the web see http://cplus.about.com/od/learningc/...ssonfive_3.htm

Last edited by 3v0; 30th August 2008 at 06:00 AM.
3v0 is online now  
Old 30th August 2008, 11:20 AM   (permalink)
Default

Thanks for the explanation.
MrNobody is offline  
Old 30th August 2008, 04:35 PM   (permalink)
Default

I would either continue to use the continue statement or use the empty bracket set as the simple semi-colon terminator seems a little ambiguous, at least to me. Seeing as how all three lines of code perform the same function you can do it any which way you want.
__________________
"Because I be what I be. I would tell you what you want to know if I
could, mum, but I be a cat, and no cat anywhere ever gave anyone a
straight answer, har har."
Sceadwian is offline  
Old 30th August 2008, 04:51 PM   (permalink)
Default

I would use the semi colon version everytime. It is only ambiguous if you are not really familiar with C.

Mike.
Pommie is offline  
Old 30th August 2008, 06:24 PM   (permalink)
Default

Quote:
Originally Posted by Pommie View Post
I would use the semi colon version everytime. It is only ambiguous if you are not really familiar with C.

Mike.
Agreed, although I slightly prefer the empty-bracket version--probably just because a professor I liked 12 years ago used it. It's unambiguous even if you are not very familiar with C.

The "continue" version I would not use or recommend. Without the enclosing braces, the semantics might get tricky for the parser. Sounds like that might be the case with Hi-Tech C.


Torben
__________________
Curiosity was framed. Ignorance killed the cat.
Torben is offline  
Old 30th August 2008, 07:27 PM   (permalink)
Default

well, I always like only the semicolons, looks really simple!!
who knows, the Hi-tech people might be using 'continue' to improve radability!! but it can be achieved better with a comment! No need for continue there..

---------------
http://dharmanitech.blogspot.com
CCD is offline  
Old 30th August 2008, 07:43 PM   (permalink)
Default

You say potato, I say potato. Wait... that little phrase doesn't work in text =\
__________________
"Because I be what I be. I would tell you what you want to know if I
could, mum, but I be a cat, and no cat anywhere ever gave anyone a
straight answer, har har."
Sceadwian is offline  
Old 30th August 2008, 07:44 PM   (permalink)
Default

Quote:
Originally Posted by Sceadwian View Post
You say potato, I say potato. Wait... that little phrase doesn't work in text =\
Yeah, as far as ';' vs '{}' it's. . .er. . .6 of one, half a dozen of the other (there, found one for text!) .

The 'continue' one just looks suspicious to me, although gcc happily compiles it in ANSI mode. Huh.


Torben
__________________
Curiosity was framed. Ignorance killed the cat.
Torben is offline  
Old 30th August 2008, 08:30 PM   (permalink)
Default

I guess it's 'safer' to explicitly refer to the continue rather than rely on the compiler knowing what to do with it. It's a whole lot of gray area =)
__________________
"Because I be what I be. I would tell you what you want to know if I
could, mum, but I be a cat, and no cat anywhere ever gave anyone a
straight answer, har har."
Sceadwian is offline  
Old 31st August 2008, 05:29 AM   (permalink)
Default

I'd say it's a pretty broken compiler that gets while(GODONE); wrong, as long as GODONE is considered volatile (which I'm pretty sure it is).

Note that this may not work with a declared variable unless it was declared as volatile. An optimizer may get overzealous in the following case
Code:
unsigned char foo;
...
foo = 0;
while(foo);
...

Last edited by philba; 31st August 2008 at 05:31 AM.
philba is offline  
Old 31st August 2008, 08:14 AM   (permalink)
Default

Perhaps they use the continue as a means to avoid optimisation simply removing the entire statement.

I have seen things happen with optimisers like that with simple tests. It's always best to make sure the controlling statement uses a variable declared as volatile, especially if it can be changed by external influences, IRQ's for instance.

Ray
zathras is offline  
Old 1st September 2008, 12:14 AM   (permalink)
Default

Quote:
Originally Posted by philba View Post
I'd say it's a pretty broken compiler that gets while(GODONE); wrong, as long as GODONE is considered volatile (which I'm pretty sure it is).

Note that this may not work with a declared variable unless it was declared as volatile. An optimizer may get overzealous in the following case
Code:
unsigned char foo;
...
foo = 0;
while(foo);
...
Oh.. umm.. thanks.. i didn't know that.. I'll try to remember to use 'continue' next time i use a non volatile variable.. If not i'll be wondering why my code is not working as it should. Thanks alot.

Last edited by MrNobody; 1st September 2008 at 12:14 AM.
MrNobody is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
New robotics tech mous3 Robotics Chat 0 31st July 2008 03:07 AM
ICD2 woes continue... Hank Fletcher Micro Controllers 12 16th June 2008 01:55 PM
Greetings Electro-tech. xl5 Chit-Chat 4 29th December 2007 07:06 PM
a solution for a bell to continue playing music after releasing switch Fahime General Electronics Chat 3 9th November 2006 11:26 AM
Electro-tech map!! jrz126 Chit-Chat 83 12th January 2006 11:43 AM



All times are GMT. The time now is 04:01 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker