Author Topic: Statement with no effect  (Read 12790 times)

0 Members and 1 Guest are viewing this topic.

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Statement with no effect
« 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);
   }


« Last Edit: February 16, 2009, 02:06:44 AM by pomprocker »

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Statement with no effect
« Reply #1 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--;
}
« Last Edit: February 16, 2009, 02:09:40 AM by cosminprund »

Offline HDL_CinC_Dragon

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: Statement with no effect
« Reply #2 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
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: Statement with no effect
« Reply #3 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) {

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Statement with no effect
« Reply #4 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;
    }
  }

Offline Pratheek

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 125
  • Helpful? 3
Re: Statement with no effect
« Reply #5 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);
}




Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
Re: Statement with no effect
« Reply #6 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) {

Offline cosminprund

  • Robot Overlord
  • ****
  • Posts: 284
  • Helpful? 8
Re: Statement with no effect
« Reply #7 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.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,659
  • Helpful? 169
    • Society of Robots
Re: Statement with no effect
« Reply #8 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 . . .

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Statement with no effect
« Reply #9 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.
« Last Edit: February 18, 2009, 10:03:17 AM by chelmi »

 


Get Your Ad Here