Society of Robots - Robot Forum

Software => Software => Topic started by: pomprocker on February 16, 2009, 02:03:23 AM

Title: Statement with no effect
Post by: pomprocker on February 16, 2009, 02:03:23 AM
I'm getting a warning 'Statement with no effect' regarding this code:
Code: [Select]
for (delay_sec; delay_sec > 0; delay_sec--) {
        delay_ms(1000);
   }


Trying to get delays in seconds here without overflowing the delay_ms function.



EDIT: I rephrased my coding, which seems the same to me but it worked:
Code: [Select]
   for (int i = 0; i < delay_sec; i++) {
        delay_ms(1000);
   }


Title: Re: Statement with no effect
Post by: cosminprund on February 16, 2009, 02:07:05 AM
You probably want something like this:

for (delay_sec=0; delay_sec > 0; delay_sec--) {
        delay_ms(1000);
}

EDIT: (I can EDIT too :D )

You might also want:

while (delay_sec>0) {
  delay_ms(1000);
  delay_sec--;
}
Title: Re: Statement with no effect
Post by: HDL_CinC_Dragon on February 16, 2009, 02:11:53 AM
Im going to assume its because the first parameter in the for loop is asking for its own integer rather than one thats already been made elsewhere. int i = delay_sec would probably work best in that situation. not 100% sure on that one though... more like 85% sure heh
Title: Re: Statement with no effect
Post by: pomprocker on February 16, 2009, 02:16:48 AM
Sorry my variable delay_sec was defined in the function argument

Code: [Select]
void resetChip(int delay_sec) {
Title: Re: Statement with no effect
Post by: cosminprund on February 16, 2009, 02:28:16 AM
Try the while loop, it might save a variable (that' important on an MCU).
If you don't want anything in the initialisation part of your for statement then write nothing, you wrote the name of the variable as if it were a declaration of some sort.

This is legal C code:
Code: [Select]
  for (;delay_sec>0;delay_sec--) {
  }

In fact this is also legal code and is an alternative to the "while(1)" everyone uses:
Code: [Select]
  for(;;) {
  }

Any for statement can be decomposed like this:
Code: [Select]
  for(initial;condition;step) instruction; 

Code: [Select]
  {
    initial;
    while(condition) {
      instruction;
      step;
    }
  }
Title: Re: Statement with no effect
Post by: Pratheek on February 16, 2009, 06:02:01 AM
I'm getting a warning 'Statement with no effect' regarding this code:
Code: [Select]
for (delay_sec; delay_sec > 0; delay_sec--) {
        delay_ms(1000);
   }

The error, I think, in the code is that you have not assigned a value to "delay_sec" in the for loop.
Since it has already been assigned previously and you wish to use that value, just skip the initialization expression in your for loop.

The correct code should be,

Code: [Select]
for (  ; delay_sec > 0; delay_sec--) // omit the initialization expression
{
        delay_ms(1000);
}



Title: Re: Statement with no effect
Post by: Admin on February 18, 2009, 01:19:56 AM
You get the warning 'Statement with no effect' because your code by default doesn't do anything:

Code: [Select]
for (delay_sec; delay_sec > 0; delay_sec--) {
        delay_ms(1000);
   }

since delay_sec in your code is initialized as 0 during compilation:
Code: [Select]
void resetChip(int delay_sec) {
So the compiler is like, why should I do a for loop for delay_sec > 0 when delay_sec=0? Your code is legit, it's just the compiler is a bit confused . . .

Of course your working code would have the same problem too, so the compiler probably just has a bug in its warning system . . .
Code: [Select]
for (int i = 0; i < delay_sec; i++) {
        delay_ms(1000);
   }

Maybe if you did this it might work? Not sure if this is legit with the compiler either, too lazy to test . . .
Code: [Select]
void resetChip(int delay_sec=10) {
Title: Re: Statement with no effect
Post by: cosminprund on February 18, 2009, 01:33:37 AM
You get the warning 'Statement with no effect' because your code by default doesn't do anything:

Code: [Select]
for (delay_sec; delay_sec > 0; delay_sec--) {
        delay_ms(1000);
   }

since delay_sec in your code is initialized as 0 during compilation:
Code: [Select]
void resetChip(int delay_sec) {
So the compiler is like, why should I do a for loop for delay_sec > 0 when delay_sec=0? Your code is legit, it's just the compiler is a bit confused . . .

No-no. There's no "delay_sec=0" code in pomprocker's code; Your brain sees it because you expect it in the begining of the for statement, but there's no such thing. The loop initialisation in the for statement is not valid because it just says the name of the variable, no equal sign, no-nothing :) The compiler is spot-on.
Title: Re: Statement with no effect
Post by: Admin on February 18, 2009, 02:19:12 AM
No-no. There's no "delay_sec=0" code in pomprocker's code; Your brain sees it because you expect it in the begining of the for statement, but there's no such thing. The loop initialisation in the for statement is not valid because it just says the name of the variable, no equal sign, no-nothing :) The compiler is spot-on.
The gcc compiler initializes non-initialized variables as 0. But not sure if it's all variables or just non-global or what . . .
Title: Re: Statement with no effect
Post by: chelmi on February 18, 2009, 09:59:58 AM
No-no. There's no "delay_sec=0" code in pomprocker's code; Your brain sees it because you expect it in the begining of the for statement, but there's no such thing. The loop initialisation in the for statement is not valid because it just says the name of the variable, no equal sign, no-nothing :) The compiler is spot-on.
The gcc compiler initializes non-initialized variables as 0. But not sure if it's all variables or just non-global or what . . .

It initializes non-initialized global variables to 0. Local variables are uninitialized. The value might be 0 is the memory was previously set to 0. But there is no guaranty.
Pratheek and Cosmicprund are right, the statement with no effect is to warn you that the initialization of his for loop ("delay_sec") has no effect (it is not initialized).

Chelmi.