简体   繁体   中英

for loop init variable scope between c++, java, C#

I have a problem in understanding the difference in scope of initial variable of for loop in those 4 languages C, C++, C#, Java

What I know is that in C#: for ( int i = 1; i<=5; i++) here, i is a local variable dose not appear out the scope of the for brackets. So I can use it multiple times in multiple blocks, also Java is like that, but what I was know that c and c++ not the same as i in for ( int i = 1; i<=5; i++) is declared on the level of for not in inside it.

I tried to run the below code to check if my thought correct or not:

old C:

   // old C:  error: 'for' loop initial declarations are only allowed in C99 or C11 mode                                                                 
  // note: previous definition of 'i' was here 
    for ( int i = 0; i<=5; i++)  printf ("%d",i);
    for  ( int i = 5; i>=0; i--)  printf ("%d",i);
    printf ("%d",i);

C99:

    // error: 'i' undeclared (first use in this function) 
    for ( int i = 0; i<=5; i++)  printf ("%d",i);
    for  ( int i = 5; i>=0; i--)  printf ("%d",i);
    printf ("%d",i);

old C++:

    // error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]                                                                  
    for ( int i = 0; i<=5; i++)  cout << i << endl;
    for( int i = 5; i>=0; i--)  cout << i << endl;
    cout << i;

C++11:

      for ( int i = 0; i<=5; i++)  cout << i << endl;
      for( int i = 5; i>=0; i--)  cout << i << endl;
      cout << i; error: 'i' was not declared in this scope   

Java:

       for ( int i = 0; i<=5; i++)  System.out.println(i);
       for(  int i = 5; i>=0; i--)  System.out.println(i);
       System.out.println(i); // error: 'i' was not declared in this scope

C#:

    for ( int i = 0; i<=5; i++)  Console.Write(i); // 0 1 2 3 4 5 
    Console.WriteLine("");
    for ( int i = 5; i>=0; i--)  Console.Write(i); // 5 4 3 2 1 0
    Console.WriteLine(i); // error: 'i' was not declared in this scope

I think C99, C++11, C#, Java are same while the diffrence only in old C and C++.

Is this right or not ??

Thanks

Not quite.

Only prestandard C++ (before the 1998 ANSI standard which became the ISO standard) behaves as you describe for "old C++". That changed in draft standards in about 1995, from memory, and most compilers at that time adopted the change. Before that, some C++ compilers limited scope of variables to loops as a vendor-specific extension.

C from the 1999 standard, standard C++, Java, and C# all limit scope of variables declared within a for loop to only that loop.

Standard C in 1989 (ANSI) and 1990 (ISO), prestandard C (including K&R), and prestandard C++ before 1995 do not. As noted by juanchpanza in comments, C89/C90 does not permit variable declarations within loop initialisation at all.

Actually, it is not right exactly for the pre-standart C++. Because some compilers have issue about loop variables which i can be used outside of the for loop block without redeclaration. I mean

// error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]                                                                  
for ( int i = 0; i<=5; i++)  cout << i << endl;
for( int i = 5; i>=0; i--)  cout << i << endl;
cout << i;

This may not be error in that compiler. And you may see this macro in files to protection.

#define for if(0) {} else for

After that now i is in else block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM