4

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???

Yadnesh
  • 1,307
  • 3
  • 17
  • 36
  • Thanks for d answers.i thought all variables having the same name have only single value stored in them and are local to the function and declarations – Yadnesh Nov 06 '11 at 13:10
  • 1
    The technical term for this is "shadow": you are shadowing the `i` defined before the `while` loop. gcc can warn about that with `-Wshadow`. – pmg Nov 06 '11 at 13:12

5 Answers5

8

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

Alok Save
  • 202,538
  • 53
  • 430
  • 533
7

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.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
5
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);
}
Sadique
  • 22,572
  • 7
  • 65
  • 91
3

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.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Not "most recent", but "in the most local scope", to be precise. – Karl Knechtel Nov 06 '11 at 12:54
  • I wouldn't say the "most recent" (as happens in some scripting languages), but the innermost declaration accessible in the current scope (example: `int a=4; { int a=10;} printf("%d", a);` prints 4, not 10). – Matteo Italia Nov 06 '11 at 12:55
  • 2
    "Most recent" is precisely technically correct, but it requires you to understand when all the various variables are created. Most likely, if you understood that, you wouldn't need this rule anyway. – David Schwartz Nov 06 '11 at 12:58
  • Since this is starting to get people confused, I'm gonna edit it a bit – Shahbaz Nov 06 '11 at 13:05
1

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153