Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we define a variable inside a while loop?

Tags:

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.

like image 361
Letterman Avatar asked Oct 17 '09 03:10

Letterman


1 Answers

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.

like image 164
Matt Brunell Avatar answered Sep 17 '22 17:09

Matt Brunell