You cannot use a variable in while declared inside a do / while loop

Why am I not allowed to use the variable I declared inside do and use it inside the while part of the do / while loop?

 do { index += index; int composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); // cannot resolve symbol composite 

The first time I saw this, I thought it was a compiler error. So I restarted Visual Studio several times, but still it does not recognize the variable in while .

What is wrong with the code, or is it a compiler error?

+6
source share
7 answers

Your variable is used outside of do / while (in C # cursors usually define a scope), and since it is declared inside do-while, you cannot use it outside that scope.

You can fix this by simply declaring it outside the loop.

 int composite = 0; // you are not required to set it to zero, as long // as you do not use it before you initialize it do { index += index; composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); 

Note (1), it is common practice to declare and set a variable at a time, but this is not a prerequisite if you set it before using it.

Note (2): in C # curly braces {....} define the scope of variables. You cannot use a variable outside its scope, it is no longer "visible", in fact, when it goes beyond, it is ready to collect garbage, and its contents may be undefined.


i konw this, but why can't the compiler do this

You asked about this in the comments. The answer is less trivial than it might seem. It depends a lot on the language definition. Some languages, such as Perl or older than BASIC, allow you to declare variables at any level and in any field. Other languages ​​allow you to define variable domains and require variables to be declared before they are used (C #, Java, C ++, Python). Usually, but not necessarily, it is usually for statically typed languages , since the compiler must know in advance which data type you want to put in the variable in order to do type checking.

If the compiler will force the declaration, but not the scope, this means that all variables will be global, which is difficult in large programs, as a programmer, you will need to maintain where you used the variable names and keep it unique. This is very tiring (think COBOL).

C # added the dynamic keyword, which allows variables with a dynamic type to be used, but this still requires a variable to be defined before it can be used.

The compiler can do this, but C # developers decided that a clear language is better, and in their opinion, declared variables is a good thing , because it prevents false errors and impossible code:

 // can you see the problem? va1l = 1; val1 = 2; vall = va1l + vall; 

Forcing you to declare variables and possess them prevents these and related types of errors.

+17
source

It's not a mistake. The condition inside the while is outside the block in which the variable is defined.

 int composite; do { index += index; composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); 
+7
source

the scope of this variable is inside the loop .... You cannot use it outside the loop

You should do something like this ....

 int composite; do { index += index; composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); 
+2
source

Although { ... } means a code block, all variables in this block are limited to this special area. Your condition is outside this scope, so it cannot access the variable there. Declare it outside the loop and it works:

 int composite = 0; do { index += index; composite += index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); 
+2
source

Per specification The condition variable for do .. while needs to be declared outside the else loop at each iteration, the variable will be reinitialized, which doesn not make sense.

  int composite; do { index += index; composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); 
+2
source

A more general rule: you cannot use a variable outside its scope , which in C # it {...} , for example.

  { int x = 123; ... } x = 456; // <- compile time error 

do {} while in your code simply means that the region belongs to the loop.

+2
source
  int composite; do { index += index; composite = index + 1; // more code here } while (UnsetBitmask(bitmasks, composite)); 
0
source

All Articles