# Thread: for loops - noob question

1. ## for loops - noob question

Could any help me through the flow of this loop? I understand the syntax, but I am trying to figure out the flow of the for loop and why it prints how it prints.

The output starts as:
0
1
1
2
3
5
...

why in the loop does it print zero, then two sets of 1's. When I try and follow the loop, I figure it to start 0 for the initial then 1,2...

Thanks

Code:
```#include <stdio.h>;

int main (void)
{

int firstval = 0;
int secondval = 1;
int tempvalue, count = 15;

for (count = 1; count <= 15; count++)
{
tempvalue = firstval + secondval;

printf("%d\n", firstval);
firstval = secondval;
secondval = tempvalue;

}

getchar();
return 0;

}```

2. Just follow the values of your variables... write them down for several times around the loop... step by step... you'll see what it's doing.

3. I see, I got it. thanks.

4. Originally Posted by teelnaw
I see, I got it. thanks.
You're welcome.

One of the most important things for a programmer to learn is troubleshooting... how to analyse a situtation that's gone wrong.

5. FYI :

Another part of programming is recognizing common sequences which show up in toy homework problems (and eventually toy interview questions) : Fibonacci number - Wikipedia, the free encyclopedia.

This also shows up if you do a google search for 0 1 1 2 3 5. That's useful for finding hints about other sequences (but in my experience more complex sequences just get matched up to phone numbers so it's not always going to help).

6. Don't put a semicolon after a preprocessor.

7. Originally Posted by Babkockdood
Don't put a semicolon after a preprocessor.
Wow, eyes of the eagle . Well done sir!