For loop question Is there a way to start a for loop at +20 on first itteration?

I'm using a for loop to light LEDs 2 -42 on a 2560 in sequence.

I know this could be done with an array. But can it be done with a for loop?

For the first itteration of the loop I would like to start at LED 22 or +20.
Next and all future itterations from 2.

There's not offset in a for loop correct?
I'm trying to think though the logic on this.

byte i = 22; //Initial setting of i to 22 This woud be in the global area or above setup.

void setup ()

for (i; i <=42 ; i = i + x) { // First interation of loop i starts at 22.

}

i=2; // I is now set to 2 so next itteration and all future itterations of the loop are from 2.

Woudl this work? Or is there a better way?
for (i; i <=42 ; i = i + x) {        // First interation of loop i starts at 22.

x is undefined.

You can start the for loop with whatever value you like.

If the first iteration (and only the first) is a special case then I think I would just deal with it separately rather than try to write a complex piece of code to make the general situation deal with the special case.

...R

Doug101:
I'm using a for loop to light LEDs 2 -42 on a 2560 in sequence.

I know this could be done with an array. But can it be done with a for loop?

For the first itteration of the loop I would like to start at LED 22 or +20.
Next and all future itterations from 2.

There's not offset in a for loop correct?
I'm trying to think though the logic on this.

byte i = 22; //Initial setting of i to 22 This woud be in the global area or above setup.

void setup ()

for (i; i <=42 ; i = i + x) { // First interation of loop i starts at 22.

}

i=2; // I is now set to 2 so next itteration and all future itterations of the loop are from 2.

Woudl this work? Or is there a better way?

For the second itteration would it start at 2 or 22?

Doug101:
For the second itteration would it start at 2 or 22?

For the second and all subsequent iterations, the loop starts at 2.

for (i; i <=42 ; i = i + x) {        // First interation of loop i starts at 22.

}
i=2;  // I is now set to 2 so next itteration and all future itterations of the loop are from 2.

That should work OK, assuming x is defined. You don't need the first "i", and something like this would probably be easier to read:

byte loopstart = 20;

void loop() {
    for (i=loopstart; i<42; i+=2) {
	printf("test");
    }
    loopstart = 2;
}

I would follow Robin2 and create a first-case statement followed by the loop. If you want to stick to the loop, another option would be

for(i = 1; i <= 42; i++)
{
    // some arbitrary variable, call it X
    X = (i == 1)?22:i;
}

Doug101:
Woudl this work? Or is there a better way?

Using magic numbers... shame shame

Doug101:
I know this could be done with an array.

the array is the better way, IMHO. any time you are using a global variable to traverse through a for-loop you need to be asking yourself "why."

BulldogLowell:
Using magic numbers... shame shame

the array is the better way, IMHO. any time you are using a global variable to traverse through a for-loop you need to be asking yourself "why."

Why, becasue I'm learning. I alreadu stated and array would be better. But why use an array if I can use write less code and accomplish the same thing with less code?

I'm going through the logic to see if it is possible.

Doug101:
Why, becasue I'm learning. I alreadu stated and array would be better. But why use an array if I can use write less code and accomplish the same thing with less code?

I'm going through the logic to see if it is possible.

So why didn't you simply try it?

TolpuddleSartre:
So why didn't you simply try it?

I'm talkign theough the logivc to see if it is even possible. I was thinking ther might be a parameter in the For loop for the ofset.

Doug101:
I'm talkign theough the logivc to see if it is even possible. I was thinking ther might be a parameter in the For loop for the ofset.

So, why didn't you simply try it?

The for loop is very well documented.