Using a global variable as the tested condition in a for loop

Evening all,

I've just had a thought to improve a piece of code I've written. Currently, my for loop has the following structure, in that the same variable is the initialized value, the tested value, and the incremented value. Here's an example.

for(x = 0; x < 255; x++);
{
code
}

Instead, I'd like to make the tested variable a different variable to the initialized, and incremented value - so I'd end up with the following structure...

for(x = 0; measurement_val < calibration_val; x++);
{
code
}

Is there any problems with doing this? The only reason I ask, is I've only ever used for loops where all three entries in the for loop are the same variable - and that seems to be how all examples are too.

Many thanks in advance!

Can you post your code?

[edit]
deleted rest of reply, cus I was wro, wroooo, wrooooooo, damnit, not right! :smiley:
Thanks AWOL
[/edit]

I don't see a problem, but I don't see any code either.

X is a counter, and it is x that need to be compared

In some cases that may be true, but not in all. A for loop is just a special form of a while loop, and how long it iterates does not necessarily depend on the count value.

Currently I have the following;

for (heater_duty = 145; heater_duty <= 255; heater_duty++)
  {
    analogWrite(heater_pin, heater_duty);
    delay(280);
    // sample probe temp, and break for loop if temp is reached before time out
    UR_m = analogRead(UR_pin);
    if (UR_m >= UR_c)
    {
      break;
    }	
  }

However, I thought doing it the following way would be simpler...

  for (heater_duty = 145; UR_m > UR_c; heater_duty++)
  {
    analogWrite(heater_pin, heater_duty);
    delay(280);
    UR_m = analogRead(UR_pin);
  }

It seems it should work...

I don't like delays, but I don't see why it should not work, for some definitions of "work"

Thanks AWOL :slight_smile:

AWOL:
I don't like delays, but I don't see why it should not work, for some definitions of "work"

I couldn't agree more about delays - this is just part of a piece of code used to test a hardware design before committing to the design in the form of a PCB, the 'proper' software won't have a delay in it.

If the delay was not there, would your comment of "for some definition of work" still stand true, or is the delay your only concern?

Well, if you remove the delay, you won't be using a while or for loop, so the construct is pretty redundant.

Ah, can one not use methods such as that used within the blinkwithoutdelay example, within a for or while loop?

Saying that, I can't quite work out how I'd do that - perhaps a while loop inside the for loop, so that until the timer exceeds its limit a boolean is set low - and while that boolean is low, the while loop just keeps looping.

Hmm, having written that - that of course gives exactly the same effect as delay... Rethink needed for the final software...

You can do what you like in a while loop but if the condition being tested is never true or you don't exit out of it explicitly, then it is going to block execution of other code anyway.

Thinks - can you use goto to exit from a loop (JOKE !)

UKHeliBob:
Thinks - can you use goto to exit from a loop (JOKE !)

runs off to check what a goto is, and how it forms the basis of a joke :smiley:

You are pronouncing the "J" wrong, it's pronounced "Yoke" :wink: