For Loop inside While Loop

This is a discussion on For Loop inside While Loop within the C Programming forums, part of the General Programming Boards category; This simple loop construct is confusing me. I'm not seeing why the output is what it is. When I look ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    For Loop inside While Loop

    This simple loop construct is confusing me. I'm not seeing why the output is what it is. When I look at the code it seems like this should print one set of statements with x going from 0 to 4. Like this

    x =1
    -array[0] =1
    -arrary[1] = 2

    ...and so on. why is this not the case?


    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int array[5];
        int i;
        int x=0;
        while(++x < 5) {
            printf("*** x = %d\n",x); 
            for (i = 0; i < 5; i++) { 
                array[i] = x; 
                printf("\t - array[%d] = %d\n", i, array[i]); 
             }
             printf("*** \n");
        }
        return 0;
    }
    OUTPUT
    Code:
    *** x = 1
    	 - array[0] = 1
    	 - array[1] = 1
    	 - array[2] = 1
    	 - array[3] = 1
    	 - array[4] = 1
    *** 
    *** x = 2
    	 - array[0] = 2
    	 - array[1] = 2
    	 - array[2] = 2
    	 - array[3] = 2
    	 - array[4] = 2
    *** 
    *** x = 3
    	 - array[0] = 3
    	 - array[1] = 3
    	 - array[2] = 3
    	 - array[3] = 3
    	 - array[4] = 3
    *** 
    *** x = 4
    	 - array[0] = 4
    	 - array[1] = 4
    	 - array[2] = 4
    	 - array[3] = 4
    	 - array[4] = 4
    ***

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,828
    Quote Originally Posted by mesmer
    why is this not the case?
    Because x remains the same in the inner loop, so on each iteration of the for loop, you are assigning the same value of x to the corresponding element of the array named array.

    It looks like you do not actually need x, since i serves the same purpose. You just need to make it such that you assign i+1 to the ith element of array. This also means that you only need a single loop, not nested loops.
    Last edited by laserlight; 11-29-2008 at 11:31 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 04:46 PM
  2. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  4. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 11:50 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21