0

I know there are plenty of answers out there but non of them cleared my doubt... C++ standard says:

The for statement

   for ( init-statement condition ; expression ) statement 

is equivalent to

   {
       init-statement 
       while ( condition ) {
           statement 
           expression ;
       }  
   }

except that names declared in the init-statement are in the same declarative-region as those >declared in the condition (which is also the scope of statement).

It means if I declare a variable inside the init-statement, then that variable will be in the same scope as that of statement and the statement executes in each iteration... now, my question is:

  1. Does variable declaration in init-statement also executed in each iteration?

for eg:

    for(int i = 0; i < 5; i++){
        int number = 2;   // this will be executed in each iteration
    }

Now, as standard says 'i' variable will also be in the same scope of 'number' so does it also destroyed and declared at each iteration like 'number'?

PS: but it can't be because standards also says that init statement executes one and only once before the condition statement.. I just want to know the reason and how things work?

EDITED

for(int i=0; i<10; i++){
    int num = 2; 
}

acc. to the standards The 'i' variable and the 'num' variable both are in the same scope then how 'i' variable is declared only once and num variable declared repeatedly (here 10 times), I mean if they both are in the same scope isn't 'i' variable too destroyed and declared 10 times like 'num'...(I know it may sound silly but it is confusing me a lot)

4
  • Why do you think that 2 variables being in the same declarative region affects when they are initialized? Commented Nov 15, 2020 at 14:01
  • Note that the exception refers to where names are declared, not where objects are created or destroyed. Commented Nov 15, 2020 at 14:36
  • @molbdnilo but usually where the name is declared, the object is created at the same place..isn't it? Commented Nov 15, 2020 at 14:56
  • What the exception means is that the scope of the declared names (which aren't necessary variables) are inside the loop, but any declared variable has a lifetime as if it were declared in a separate scope (the outer braces) outside of it. (Names have scope, objects have lifetime.) Commented Nov 15, 2020 at 16:05

3 Answers 3

1

The exception is mostly to pinpoint that

for (int i = 0; i != 42; ++i)
{
    int i = 3; // error: redeclaration of i
}

whereas

{
    int i = 0;
    while (i != 42) {
        int i = 3; // "Ok", hide outer i

        ++i; // actually modify inner i
    }
}

That doesn't affect lifetime of variable or place of initialization.

Sign up to request clarification or add additional context in comments.

4 Comments

but exception is saying that variable declared in init-statement and variable declared in loop statement lies in the same scope...but the equivalent code(while loop which compiler generates, I think) has two different scopes (one outer bracket scope and one inner bracket scope ) of 2 'i' variables...but standard says they both have the same scope.( that's why we can not declare another variable of same name in loop-statement )
I have edited my question (added the last paragraph) can you clarify my doubt please..
It is an exception according to normal scope, they act they are in different scope (for lifetime/creation), they act as same scope about naming.
you mean the code in the last para will generate this while loop... { while(condition){ int i=0; int num = 2; // as i should be in the same scope as that of num. } } `
0

As standard says, i will be in same scope as number, but if you have a look at the code that standard provides, i will be initialized only once. number, on the other hand, will be initialized in each iteration.

Comments

0

See the equivalent while loop. Variable declared in the initiator outlives all instances of variables inside the loop. But once the for loop ends, the initial variable gets out of scope and dies(gets destructed). One thing to note is if can also define variables in condition part:

if(bool x=foo()) /*...*/;
if(int x=bar(); x>3) /*...*/; 

and similar rule applies; You'll get an implicit scope around if/while statement. But there is difference between while and for.In contrast to for, the object initiated inside a while condition:

while(bool x=foo()) /*...*/;

is actually scoped inside the loop and gets destructed/constructed per iteration

Mind the term scope. Roughly speaking, Scope is any pair of curly braces ({}) that forms a block statement. But there are implicit scopes too - just like the ones around for/while/if. Any scope that gets exited, kills its nested objects.

regards, FM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.