Thread: program explanation

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    program explanation

    Code:
    #include <stdio.h>
    
    void main()
    {
    	int a,b;
    
    	for(a=0;a<6;++a){
    		for(b=0;b<=a;++b)
    			putchar('*');
    			putchar('\n');
    	}
    }
    This generates the figure:
    *
    **
    ***
    ****
    *****
    ******
    Could someone dissect each for statement and the entire loop as well?
    What I don't get is how it knows to print just 1 * first, then 2 ...
    Sorry for being such a newbie ... and thanks.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    hmm, read a book on the for statement, then take about 5 minutes to look at the tiny piece of code and you will figure it out and have a better understanding of for loops!

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Your indentation is lying, the indentation that reflects what is actually happening is like this:
    Code:
    #include <stdio.h>
    
    int main(void) /* main returns int, not void */
    {
        int a,b;
        for (a = 0; a < 6; ++a)
        {
            for (b = 0; b <= a; ++b)
                putchar('*'); /*only one statement in the inner for, because there are no braces */
            putchar('\n');
        }
        return 0;
    }
    Make more sense now?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    CWR - thanks ... and not really, but i copied it down and i'll analyze it.

    rockytriton - what book would you suggest? for beginners and with lots of sample programs and exercises i can study?

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by markg
    rockytriton - what book would you suggest? for beginners and with lots of sample programs and exercises i can study?
    You can study tutorials on this site itself.....good collection of tutorials are available on this site

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    any book that contains the phrases "c++" and "beginner", or like sunny suggest, use a tutorial, but not all tutorials will actually explain how things work well.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Work through the code in your head, going through each line and seeing what happens to a and b. If that is too difficult, add some diagnostic printf's to show the values of a and b, or run it through a debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM