"else" instruction doesnt get xcuted

Hi All,
Using uVision simulator and Keil arm or
GNU compiler, the "else" instruction never gets xcuted. Using MSVC++ it does get xcuted.

Anyone know why?? puzzleing.
Thanks
----------------------------
int main (void){
unsigned int i=0,result=0;

for (i=0;i<5;i++){
if (i>3)
result=i*10;
else
result=i*5;
}

return(1);
}//main
--------------------------------

Parents
  • The loop always runs until i>3, so the compiler is smart enough to realise that it doesn't need to bother with the 'else' clause, because its result will always be overwritten by the 'then' clause!

    In fact, you never use 'result' for anything - so the compiler could actually optimise the entire function to:

       return(1);
    and it would make no material difference to the operation of the system!

    No doubt you could find an MSVC optimisation setting that did the same...

    You could try making 'result' volatile...?

    BTW: what do you think will happen to the return value from main...?

Reply
  • The loop always runs until i>3, so the compiler is smart enough to realise that it doesn't need to bother with the 'else' clause, because its result will always be overwritten by the 'then' clause!

    In fact, you never use 'result' for anything - so the compiler could actually optimise the entire function to:

       return(1);
    and it would make no material difference to the operation of the system!

    No doubt you could find an MSVC optimisation setting that did the same...

    You could try making 'result' volatile...?

    BTW: what do you think will happen to the return value from main...?

Children
More questions in this forum