Thread: for loops - noob question

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    I see, I got it. thanks.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by teelnaw View Post
    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. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Don't put a semicolon after a preprocessor.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Babkockdood View Post
    Don't put a semicolon after a preprocessor.
    Wow, eyes of the eagle . Well done sir!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM