While () without {}?
I had this code in a project in VS2010 - it was a placeholder method that I hadn't fully implemented yet. I started implementation today. Note that there is no {} surrounding the if / else for the while statement. This has been compiled many times - it has been that way for quite some time now. Is this a bug in VS? I thought all loops were needed {}
private void ParsefCIPProfiles(string block)
{
StringReader reader = new StringReader(block);
string readline = reader.readline();
while (readline != null)
if ()
{}
else
{}
}
source to share
See the C # formal grammar: http://msdn.microsoft.com/en-us/library/aa664812(v=vs.71).aspx
while-statement:
while ( boolean-expression ) embedded-statement
embedded-statement:
block
empty-statement
expression-statement
selection-statement
iteration-statement
jump-statement
try-statement
checked-statement
unchecked-statement
lock-statement
using-statement
selection-statement:
if-statement
switch-statement
if-statement:
if ( boolean-expression ) embedded-statement
if ( boolean-expression ) embedded-statement else embedded-statement
It is important to note that the while statement requires an inline statement, and the if statement is a select statement that is inline.
You can get your code example using these statements.
source to share
Curly braces are usually unnecessary if there is only one statement, for example:
if(readline != 0)
doSomething();
Should work because the doSomething () function is in the scope of the if loop. However, I would have expected this code to look like if there were curly braces in there (to define the scope boundaries):
while (readline != null) {
if ()
{ }
}
else
{}
NOW else is outside the scope of the while loop, and I see no reason why it won't compile. He just can't behave the way you imagine.
However, the above code is not entirely correct. This doesn't perfectly mimic your code. The if-else statements are considered one of the statements. Therefore, the area boundaries actually look like this:
while (readline != null) {
if ()
{ }
else
{}
}
Edit: Scope is the context in which the identifier can be used. Thus, the scope of a variable depends on how it is defined and where. This means that the variable can only be used in certain places. The scope of a loop is everything inside its curly braces; if it doesn't have curly braces, then only the next statement is within the scope of the loop.
This code will work:
while(readline != null)
if(readline == 1)
doSomething();
This code works because doSomething () is in the scope of the if statement, which is also in the scope of the while loop.
source to share
As many other posters have pointed out, the parentheses are unnecessary because the while loop only contains one statement. if-else
counts as a single statement, even if split across multiple lines.
However, I always include parentheses around all nested statements for the following reason. I have the following code:
while (readline != null)
if (foo = true)
{ DoSomething(); }
else
{ DoSomethingElse(); }
Then I decided I wanted to add another statement to my loop while
:
while (readline != null)
if (foo = true)
{ DoSomething(); }
else
{ DoSomethingElse(); }
DoYetAnotherThing();
Oh, look at my mistake? The call DoYetAnotherThing()
will be executed after the loop finishes, which I don't want because I haven't included the curly braces. If I had included them from the beginning, I would not have this problem. So I think it's generally a good idea to always include them, even when there is only one statement, it helps to avoid mistakes.
There's also some confusion that can be caused by the freeze-up problem when you don't use curly braces, but I'll stop incoherently and let you do your research.
source to share