variable declaration within the while loop C/C++

后端 未结 5 1642
失恋的感觉
失恋的感觉 2020-12-17 06:24

according to me following while loop should be infinite but it runs only thrice

   main()
   {
   int i=3;       
   while(i--)
    {
      int i=100;
               


        
相关标签:
5条回答
  • 2020-12-17 06:30

    Every scope in C++ (roughly speaking, each pair of braces that's not used for a special purpose such as array initialization) may contain its own local variable declarations. Writing int i = 100; within the loop specifies another variable named i that is different from the one outside the loop, and causes code within the scope that uses i, by default, to refer to the inner i instead of the outer one. However, the i-- in the loop condition still uses the outer i.

    When you replace int i = 100 with i = 100, now there is only one variable, which gets set to 100, decremented twice (once inside the loop and once by the loop itself), and re-set to 100, repeatedly.

    0 讨论(0)
  • while(i--)
        {
          int i=100; // gets created every time the loop is entered
          i--;
          printf("%d..",i);
        } // the i in the loop keeps getting destroyed here
    

    Why dont you also try:

    while(i--)
    {
    
                  {
                      int i=100; //Visible only in this scope
                      i--;
                      printf("inner i=%d..",i);
                  } //gets destroyed here
            printf("\nouter i=%d..\n",i);
    }
    
    0 讨论(0)
  • 2020-12-17 06:51

    The variable i that checks the condition is the one you declared in main() not the one inside the loop.

    Both are different variables you are confusing them as one, the compiler doesn't get confused as easily as you were.

    Inside the loop i refers to the one you declared inside the { } but outside the { } the i refers to the one declared in main()

    0 讨论(0)
  • 2020-12-17 06:55

    The variable i in while(i--) is different from the variable i defined inside the loop.

    Basically int i = 100 shadows the previous int i = 3 and inside the block of the while you're referring to a new variable.

    At the end of the day, I find no plausible scenario where you would need to do something like this.

    0 讨论(0)
  • 2020-12-17 06:55

    Every variable refers to its most recent declaration (that is valid in that scope of course):

    main()
    {
       int i=3;       
       while(i--)             // this i is the one defined in the line above
       {
           int i=100;
           i--;               // this i is the one defined in the line above
           printf("%d..",i);  // this i is the one defined two lines above
       }
    }
    

    So your while loop iterates 3 times because it depends on the i that is declared by int i = 3; Inside the loop it prints 99 because there i refers to the i that is declared by int i = 100;, which is --ed.

    If you change int i = 100; to i = 100, then you are changing the first i and not introducing another variable. Hence the infinite loop.

    Edit Some people said instead of "most recent" I should say "innermost declaration accessible in the current scope" giving this example:

    int a=4;
    {
        int a=10;
    }
    printf("%d", a);
    

    Since the second a is not visible by printf, obviously printf("%d", a); cannot refer to it. I assumed the reader knows enough to know a variable is accessible only inside the scope it is defined in. Otherwise, yes the phrases in the first two comments are more precise.

    0 讨论(0)
提交回复
热议问题