Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use variable in while clause that is declared inside the do/while loop

Tags:

c#

do-while

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

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

First time I saw this I thought it was a compiler bug. So I restarted Visual Studio multiple times but still it does not recognize the variable in while.

What is wrong with the code, or is this a compiler bug?

like image 461
M.kazem Akhgary Avatar asked Sep 01 '15 14:09

M.kazem Akhgary


4 Answers

Your variable is used outside the do/while (in C# the curlies typically define the scope) and since it is declared inside the 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 in one go, but this is not a requirement, as long as you set it before you use it.

Note (2): in C#, curly braces {....} define the scope of variables. You cannot use a variable outside its scope, it is not "visible" anymore, in fact, once it goes out of scope, it is ready to be garbage collected and its contents can be undefined.


i konw this,but why compiler cant do this

You asked this in the comments. The answer is less trivial than it may seem. It highly depends on the language definition. Some languages, like Perl, or older BASIC, allow variables to be declared at any level and in any scope. Other languages allow scoping of variables and require variables to be declared prior to their use (C#, Java, C++, Python). Usually, but not necessarily, this is common for statically typed languages, as the compiler needs to know beforehand what type of data you want to put in the variable, in order to do type checking.

If a compiler would force declaration, but not scope, it means that all variables would be global, which is hard in larger programs, as as a programmer, you will have to maintain where you used what variable names and maintain uniqueness. This is very tedious (think COBOL).

C# added the dynamic keyword, which allows for variables that have dynamic type, but this still requires you to define the variable prior to its use.

A compiler can do this, but the designers of C# have decided that clear language is better, and in their opinion, declared variables are a good thing, because it prevents spurious errors and impossible-to-follow code:

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

Forcing you to declare variables and to have them scoped prevents this and related kinds of errors.

like image 94
Abel Avatar answered Sep 19 '22 15:09

Abel


This is not a bug. The condition inside the while loop is outside of the block in which the variable is defined.

int composite;
do
{
   index += index;
   composite = index + 1;
   // more code here
} while (UnsetBitmask(bitmasks, composite));
like image 25
Sam Avatar answered Sep 20 '22 15:09

Sam


the scope of this variable is just 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)); 
like image 21
zeeshan sarfraz Avatar answered Sep 21 '22 15:09

zeeshan sarfraz


While { ... } means a codeblock all variables within that block are limited to that special scope. 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));
like image 43
MakePeaceGreatAgain Avatar answered Sep 20 '22 15:09

MakePeaceGreatAgain