Thread: simple question about variables and loops

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    simple question about variables and loops

    Code:
    {
    	int i, j;
    
    	i = 1;
    	/* j = 1;  */
    
    
    		
    	while (i <=3){
    		
    	printf("The start of iteration %d of the outer loop.\n",i);
    		
    	 j = 1; /* why this should be here and not under i=1;*/
    		do 
    	
    	{
    		printf("	Ieteration %d of the inner loop,\n", j, i);
    		j++;
    	
    	}
    	while (j<=4);
    	i++;
    	printf("The end of iteration %d of the outer loop.\n", i);
    	}
    	return 0;
    }
    i have commented out where my question is, someone explain to me why j=1; should be put right before the do-while loop.
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    If you put the j=1 outside of the outer loop then it will never be reset. Each pass of the inner loop will start with j = 4, so the inner loopo will only go through once.

    Immeditely before every while loop you have to set the starting point of your loop. Since teh inner loop goes through multiple times, each time through it you have to reset the counter's starting point.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    ah that makes sense
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about loops
    By lroberts1016 in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2008, 07:01 PM
  2. declaring variables inside loops
    By samus250 in forum C Programming
    Replies: 21
    Last Post: 04-30-2008, 01:51 PM
  3. how to access local variables outside loops?
    By cdkiller in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2006, 06:48 PM
  4. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  5. Issues with variables and for loops
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 01-10-2004, 08:48 AM