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
  {}
}

      

+3


source to share


8 answers


No, this is not a mistake. In fact, for single-claim content, most other visible-range operators do not require curly braces either. For example:



//This is valid
using (var f = new foo)
    f.Bar();

// So is this
foreach (var i in someInts)
    Console.Out.WriteLine(i);

      

+8


source


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.

+4


source


There is nothing wrong with the following statement:

while (readline != null)
    if ()
    { }
    else
    { }

      

if ... else

is one statement and will continue until readline

it is null.

+3


source


Only if multiple statements exist and you want all of those statements to be part of a loop.

There is only one if else statement, which is part of the loop. Anything after that won't be part of the loop, and if you want more statements, wrap them in{..}

In this case for

, if

etc.

+1


source


You can omit curly braces when there is only one statement in the loop. Or in the instructions if

/ else

.

+1


source


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.

+1


source


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.

+1


source


while()

without { }

will execute the next single statement. if else

is a single statement, so it will be executed until readline

it is null.

+1


source







All Articles