[英]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 我在理解这4种语言C,C ++,C#,Java中for循环的初始变量范围不同时遇到问题
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. 我知道的是,在C#中:
for ( int i = 1; i<=5; i++)
在这里, i
是局部变量,不会出现在for括号的范围之外。 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. 所以,我可以多次使用它在多个块,还Java是这样的,但我是知道,C和C ++不一样,
i
在for ( int i = 1; i<=5; i++)
的声明不在里面的水平。
I tried to run the below code to check if my thought correct or not: 我试图运行以下代码来检查我的想法是否正确:
old C: 旧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: 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++: 旧的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: 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: 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#: 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++. 我认为C99,C ++ 11,C#,Java是相同的,而区别仅在于旧的C和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++". 只有标准的C ++(在1998年的ANSI标准成为ISO标准之前)的行为与您对“旧的C ++”的描述相同。 That changed in draft standards in about 1995, from memory, and most compilers at that time adopted the change.
大约在1995年,标准草案从内存中发生了变化,当时的大多数编译器都采用了这种变化。 Before that, some C++ compilers limited scope of variables to loops as a vendor-specific extension.
在此之前,一些C ++编译器将变量的范围限制为循环,作为特定于供应商的扩展。
C from the 1999 standard, standard C++, Java, and C# all limit scope of variables declared within a for loop to only that loop. 来自1999年标准的C,标准C ++,Java和C#都将在for循环中声明的变量的范围限制为仅该循环。
Standard C in 1989 (ANSI) and 1990 (ISO), prestandard C (including K&R), and prestandard C++ before 1995 do not. 1989年(ANSI)和1990年(ISO)的标准C,1995年之前的标准C(包括K&R)和标准C ++都没有。 As noted by juanchpanza in comments, C89/C90 does not permit variable declarations within loop initialisation at all.
正如juanchpanza在评论中指出的那样,C89 / C90完全不允许在循环初始化内进行变量声明。
Actually, it is not right exactly for the pre-standart C++. 实际上,这对于标准的C ++来说并不完全正确。 Because some compilers have issue about loop variables which
i
can be used outside of the for loop block without redeclaration. 因为某些编译器存在关于循环变量的问题,
i
可以在for循环块之外使用该循环变量而无需重新声明。 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. 在那之后,现在
i
在else块中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.