为什么我们不能定义一个while循环中的变量? [英] Why can't we define a variable inside a while loop?

查看:488
本文介绍了为什么我们不能定义一个while循环中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以这样做:

 使用(流S ..)

 的for(int我...)

我们为什么不能也做这样的事情:

 ,而((INT I = NextNum())0){..}

我发现它非常有用和明智的。


解决方案

我不是一个语言的设计师,但我给它一个受过教育的猜测。

内的子句而()执行循环的每一次执行。 (末尾+1更多的时间。)语句 INT I = NextNum()声明了一个局部变量。你不能声明一个局部变量超过一次。

更新

语义,这是有道理的,这应该是可能的。事实上,正如在评论中指出的那样,这是可能的其他语言。然而,这不会没有重新编写一些主要的语法规则是在C#可能

局部变量必须在一份声明中声明。我相信语言,因为不是真正执行的变量声明这种方式分开。当你看到一行code的创建一个变量,并为其分配一个值,实际上仅仅是两个语句的快捷方式。从 ECMA-334规格的:


  

这个例子


 无效F(){
   INT X = 1,Y,Z = X * 2;
}


  

正好对应


 无效F(){
   INT X; X = 1;
   诠释Ÿ;
   INT Z者除外; Z = X * 2;
}

本身的变量声明部分没有执行。这只是意味着应该在栈上分配一定类型的变量一些内存。

,而语句期待一个布尔值前pression,但一名前pression不能由一个声明 - 不需要特殊的外壳了一些新的语法。​​

循环是专门设计来声明一个局部变量,但你会注意到,声明部分是执行只有一次。
使用语句是专门设计来声明局部变量(以及它们的处理)。它也被执行只有一次。

也认为这是一个局部变量声明没有返回值 - 它不能,因为它可以让你声明多个变量。将这种说法返回的值?

  INT X = 1,Y,Z = X * 2;

上面的语句是一个局部变量声明的。它是由一种类型的,和三个的局部变量声明符的。这些中的每一个可任选地包括一个=标记和一个局部变量初始化要允许一个局部变量来以这种方式进行声明意味着你拉开现有语法比特,因为你将需要的类型说明符,但要求一个单一的声明符,这样它可以返回一个值。

启用此行为可能中堂负面影响也认为,,而不要 / ,而语句是相反的,但平行。作为一个语言设计者,你会不会也让<$ ​​C $ C>做语句来声明一个局部变量?我不认为这是可能的。因为它不会被初始化尚未(如在第一次运行),你将不能够使用该变量在循环体。只有while语句是可能的,但你会同时和不要语句破坏之间的的并行性。

We can do:

using (Stream s ..)

and:

for (int i ...)

Why can't we as well do something like:

while ((int i = NextNum()) > 0) {..}

I find it very useful and sensible.

解决方案

I'm not a language designer, but I'll give it an educated guess.

The clause inside the while() is executed every single time the loop is executed. (+1 more time at the end.) The statement int i = NextNum() declares a local variable. You can't declare a local variable more than once.

Update

Semantically, it makes sense that this should be possible. In fact, as pointed out in the comments, this is possible in other languages. However, this would not be possible in C# without re-writing some major syntax rules.

Local variables must be declared in a statement. I believe the language to be separated this way because a variable declaration is not really executed. When you see a line of code that creates a variable and assigns it a value, that is actually just a shortcut for two statements. From the ECMA-334 Specification:

The example

void F() {
   int x = 1, y, z = x * 2;
}

corresponds exactly to

void F() {
   int x; x = 1;
   int y;
   int z; z = x * 2;
}

The variable declaration part by itself is not "executed". It just means that there should be some memory allocated on the stack for a certain type of variable.

The while statement is expecting a boolean expression, but an expression cannot be composed of a statement -- without a special casing some new grammar.

The for loop is specially designed to declare a local variable, but you'll note that declaration part is "executed" only once. The using statement was specifically designed to declare local variables (and dispose of them). It is also "executed" only once.

Also consider that a local variable declaration doesn't return a value -- it can't since the it allows you to declare multiple variables. Which value would this statement return?

int x = 1, y, z = x * 2;

The above statement is a local-variable-declaration. It is composed of a type, and three local-variable-declarators. Each one of those can optionally include an "=" token and a local-variable-initializer To allowing a local variable to be declared in this manner would mean that you pull apart the existing grammar a bit since you would need the type specifier, but mandate a single declarator so that it could return a value.

Enabling this behavior may nave negative side effects also, Consider that the while and do/while statements are opposite, but parallel. As a language designer, would you also enable the do statement to declare a local variable? I don't see this as possible. You wouldn't be able to use the variable in the body of the loop because it wouldn't have been initialized yet (as of the first run). Only the while statement would be possible, but then you would destroy the parallelism between while and do statements.

这篇关于为什么我们不能定义一个while循环中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆