This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: When to use if-if, else-if, switch, goto and local scope vars?


Thanks for Your answers!

On 23/09/07 14:14:33, candido lopez rodriguez wrote:
> switch statement: if the case is in sequence (for example case 1: 2, etc)
>  the compiler will translate to a translation table (a very short operation)
> if not the compiler will use jumps (same as if/then else).

So a switch statement cannot be slower than an else-if?

>try to eliminate operations, for example
>
> if ( a == 1)
>    b=10
> else
>    b = 1
>
> change to
> b=1;
> if ( a == 1)
>     b= 10;
>
>
> about your question where to change from an if to a switch
>
> if the compare is other than a int/char use if

I'm not sure what You mean here. Aren't all comparisions int/char (equal  zero
or not)?

>
> does it save RAM when I define variables in the innermost scope possible?
> no try to reuse variables

Reuse is best sure, but what about:

void Openfile(void){
if (error){
        char s[5];
        itoa(int, s, 10);
        lcd_write_str(s);
        }
}

Is  the  char-array  only  allocated  when  the  error  occures, or will it be
allocated when the function is called, or is it  already  allocated  when  the
whole programme is loaded?

>
> - is it better to nest switch statement or use else-if instead of nesting?
>  think  about  the  logic, are the if needed to begin? the compiler needs to
generate jump operations, depending how far needs to go it needs to require  a
long jump and for your question both will be the same
>
>
> - when does it help writing out loops?
>
> speed vs memory (you mean unroll loops)

Is there a method to let the preprocessor unroll loops?

> please do not do things like I see all the time
> int count;
> for(count=0; count < function(a); count++)
> {
>       operations
> }
>
> instead
> int count;
> int limit = function(a);
> for (count=0; count < limit; count++)
> {
>    operations;
> }
>
>
> - should I concider using banned goto instructions for microcontrollers?
>
> a good book that I highly recommend is hacker delight henry s. warren junior
> ISBN-0-201-91465-4

Great, I'll have a look into it. Thank You very much.

Lynx


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]