variable declaration within the while loop C/C++

霸气de小男生 提交于 2019-12-18 04:24:18

问题


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

   main()
   {
   int i=3;       
   while(i--)
    {
      int i=100;
      i--;
      printf("%d..",i);
    }
   }

it outputs 99..99..99

but according to me it should run infinite times as every time control enters while loop it gets value 100.so it will never reach zero. just to experiment i replaced int i=100; with i=100; in side the while loop and now it runs infinite times..WHY???


回答1:


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()




回答2:


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.




回答3:


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);
}



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/8027082/variable-declaration-within-the-while-loop-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!