Thread: why does the value in for loop reset?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    18

    why does the value in for loop reset?

    Code:
    #include <stdio.h>
    Code:
    
    
    int main()
    {
        int n;
        int i;
    
        for (n=1; n<=5; n++)
            {
        for (i=0; i<n; i++) // when the code reaches this loop and prints "*" i; will be +1 i.e i=1; and eventually i; will equal to n; and the code will jump out of the loop. What I dont understand is why i; resets after jumping out of the loop. When the loop starts again, i; equals to 0 instead of being 1 smaller than n;
            {
             printf("*");
            }
             printf("\n");
            }
    
    return0;
    }
    
    



    Im not trying to achieve a different result. I just need to know why i; goes back to 0 after jumping out of the loop.



    Last edited by mangekyou; 04-02-2019 at 06:23 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I just need to know why i; goes back to 0 after jumping out of the loop.
    Because that's what the i=0 part of the for loop does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    For each iteration of the outer loop, each time the code enters the inner loop, it will initialize i to 0.

    You need to do two things:

    1) You need to study loops more thoroughly.

    2) you need to format your code better.

    Code:
    #include <stdio.h>
    
    int main()
    {
       int n = 0;   /* Initialize ALL local variables to 0, NULL, */   
       int i = 0;   /* or some other legitimate value! */
    
       for (n=1; n<=5; n++)
       {
          for (i=0; i<n; i++) 
          {
             printf("*");
          }
          printf("\n");
       }
    
       return 0;
    }
    Code:
    Output:
    
    *
    **
    ***
    ****
    *****
    What output did you expect?

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Quote Originally Posted by rstanley View Post
    What output did you expect?
    When i goes from 0 to 1, the condition for i<n is no longer met, when jumping out of the loop and changing n from 1 to 2 and then going into the inner loop, I was expecting i to still be 1 and not go back to 0. I did not know that the initialization statement goes back to its original state and I still do not know why it does that.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by mangekyou View Post
    When i goes from 0 to 1, the condition for i<n is no longer met, when jumping out of the loop and changing n from 1 to 2 and then going into the inner loop, I was expecting i to still be 1 and not go back to 0. I did not know that the initialization statement goes back to its original state and I still do not know why it does that.
    That is the nature of how a for loop executes. For each iteration of the outer loop, the inner loop executes as a new statement.

    Code:
    for(initialization; condition; update)
    {
    ...
    }
    "for (i=0; i<n; i++)"

    Step 1) The first section, "i = 0", initialization ALWAYS executes, once, when the for loop starts.
    Step 2) The second section, "i < n", the condition is tested, if true, the statements in the body of the for loop executes
    Step 3) The third section, "i++", the loop variable is incremented
    Step 4) The execution returns to Step 2, until the condition fails.

    Again, please go back to a good book on the C Programming Language, and study the chapters on for(), while(), and do-while() loops!

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    18
    Quote Originally Posted by rstanley View Post
    That is the nature of how a for loop executes. For each iteration of the outer loop, the inner loop executes as a new statement.

    Code:
    for(initialization; condition; update)
    {
    ...
    }
    "for (i=0; i<n; i++)"

    Step 1) The first section, "i = 0", initialization ALWAYS executes, once, when the for loop starts.
    Step 2) The second section, "i < n", the condition is tested, if true, the statements in the body of the for loop executes
    Step 3) The third section, "i++", the loop variable is incremented
    Step 4) The execution returns to Step 2, until the condition fails.

    Again, please go back to a good book on the C Programming Language, and study the chapters on for(), while(), and do-while() loops!
    I understand now, thanks for clearing it up. I just started studying C and Im definitely not done studying for loops. Thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does auto_ptr::reset mean?
    By meili100 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2008, 02:51 PM
  2. Trying to loop my program or reset it Dev c++
    By GameGenie in forum C++ Programming
    Replies: 8
    Last Post: 07-21-2005, 12:39 PM
  3. How to reset a pointer in a loop?
    By stillwell in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 02:06 PM
  4. reset bios
    By rajesh23 in forum C Programming
    Replies: 5
    Last Post: 10-21-2003, 05:33 PM
  5. help why does cnt not reset to 1??
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-17-2002, 12:56 AM

Tags for this Thread