"For" ? Fibonacci Blink

My ?: Why does the FOR behave this way, or am I incorrect in my conclusion?

Specifically: Ending the for loop with a "<" as opposed to an "="

This is my first Arduino Program, so pardon my ignorance. I am simply toying around with the first project in Massimo Banzi's "Getting Started with Arduino", (i.e., push a button and an LED lights up and then turns off), very simple. I decided to have some fun with the code and make the LED blink according to the Fibonacci sequence (i.e., 1 1 2 3 5 8 ...) and here's what tripped me up:

Normally a "For" statement works according to lets say for example n=1 to n=100, but I found that if you want to loop 100 times in this language you would use n=0 to n<100. In my code below, if you track to the For loop, you will see the version that works. The one where I ended the loop when the counter is less than the current Fib number, also starting the count at zero. Starting it at one and ending when it equaled the current Fib resulted in an endless loop of blinking LED. I don't comprehend how that woulkd be possible.

//example 2Mod: turn on led while the button is pressed and LED will successively blink out the Fibonnaci Serquence

const int LED = 13; // LED connected to
const int BUTTON = 7;
int val = 0;
int fib2 = 0;
int fib1 = 1;
int fib = 1;

void setup() {
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);

}

void loop() {
val = digitalRead(BUTTON);
if(val==HIGH){
if(fib==1){digitalWrite(LED,HIGH);
delay(100);
digitalWrite(LED,LOW);
delay(100);}

else{for(int n = 0; n < fib; n=n+1){
digitalWrite(LED,HIGH);
delay(100);
digitalWrite(LED,LOW);
delay(100);}}

fib = fib1 + fib2;
fib2 = fib1;
fib1 = fib;}
else{digitalWrite(LED,LOW);}
}

You've shown us the code that works. That's not very helpful.
Show us the for loop statement you used that failed.
I presume you didn't write it properly but there's no way to see it from here :wink:

Pete
P.S. Read How to post code properly before posting more code.

Ending the for loop with a "<" as opposed to an "="

The middle part of a for loop acts as a while statement, and the loop will continue while the statement is true. Could it ever continue while the variable equals a value ? You may also have compounded the problem by using = instead of == to do the comparison but we won't know until we see your code.

Why does the FOR behave this way, or am I incorrect in my conclusion?
Specifically: Ending the for loop with a "<" as opposed to an "="

The middle cause in the for loop is the test for CONTINUING the loop, not for ending it.
These three loops should be equivalent:

for (int i=0; i < fib; i++) {}  // 0 .. fib-1
for (int i=1; i <= fib; i++) {}  // 1 .. fib
for (int i=1; i != fib; i++) {}  // 1 .. fib

But THESE loops do something quite different (and probably not what you were hoping for.)

for (int i=0; i==fib; i++) {}  // loop body never executes (fib is never 0)
for (int i=0; i=fib; i++) {} // infinite loop; (i=fib) evaluates to non-0, ie TRUE.

Specifically: Ending the for loop with a "<" as opposed to an "="

You can start and end loops on ANY values, and ANY conditions you like. There is no requirement to start at 0, or the have a < condition. Each of the three expressions in the for can be as simple, or as arbitrarily complex, as you care to make it.

Regards,
Ray L.

And the while condition defaults to true, so you can say

for (;;)
{
}

which is equivalent to

while (true)
{
}

Equivalent, but less readable...

Thank all of you for your insight. I appreciate you taking the time to reply. Very Helpful.

Cheers! Jon