language agnostic - Does a for loop create two scopes? -




imagine following statement (from imaginary c-like language) gets desugared more simple form:

1| (int = 0; < 10; ++i) 2| { 3|     // work 4| } 

of course, sticking point int = 0 in initializer, beacuse suppose:

  1. after loop done, i out of scope , can't reference it.
  2. upon reaching line 2, new scope pushed on stack , popped on line 4.

this mean specific for-loop desugar to:

{     int = 0;     while (i < 10)     {         // work         ++i;     } } 

leading creation of scope sole purpose of containing incrementer variable.

i understand specifics implementation defined language permits declarations inside classic-style loops. i'm curious if how work under covers, @ least when creating initial ast.

i think thats compiler your. can see if try compile both codes on llvm e.g. clang -s -emit-llvm , compare results.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -