I recently came across this code in a project - which I assume was there by mistake:
if(condition) { //Whatever... };
Note the semi colon after the closing brace.
Does anyone know what the effect of this is?
I assume it does not have any effect, but would have thought it would have caused a compiler error.
Do not use a semicolon on the same line as an if , for , or while statement because it typically indicates programmer error and can result in unexpected behavior.
Role of Semicolon in C: Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements.
When you first start working with while statements, you might accidentally place a semicolon after the “while(true/false expression)” part of the statement such as shown below. The semicolon results in a null statement, a statement that does nothing.
while' loop syntax needs a semicolon at the end. Whereas for and while loop do not need a semi-colon terminator at end.
This is a simple question with a simple answer, but I just wanted to add something relevant. Often people understand that it does nothing and particularly for the case that you presented, the semi-colon is an unnecessary line termination.
But what is the rationale behind it ?
Actually, those empty statements are allowed for statement like these:
// Use an empty statement as the body of the while-loop. while (Method()) ;
I agree that it does nothing. But it can help certain loops conform to the syntactic requirements of the language and I think this is what people should understand from it. As other said, I agree you can remove it, I just wanted to underline why C# allows it.
Further clarification
An empty statement is used when you don't need to perform an operation where a statement is required. It simply transfers control to the end point of the statement. It has no effect at all, it is pure syntactic sugar.
As stated by @PaulF, in the example above, you could use an empty block ({}
) instead. It would be totally valid and have the same effect.
Again, it all comes down to style. You don't need it, but it can certainly help you conform to whatever rules of your coding environments.
Common use-cases (where one could see empty statements)
While loop with empty body (same case that I underlined above)
void ProcessMessages() { while (ProcessMessage()) ; // Statement needed here. }
goto
statements (rarely use but still valid)
void F() { //... if (done) goto exit; //... exit: ; // Statement needed here. }
From MSDN
Class declaration (Props to @EricLippert for bringing this one)
class SomeClass { ... };
Note that in this case, as stated by @EricLippert in the comments section, this is simply a courtesy to C++ programmers who are used to typing semis after classes; C++ requires this.
Even though the general use of empty statements is debatable mainly because of the confusion they can bring, in my opinion, syntactically speaking they have a place in C#. We must not forget that C# is an increment of C++ (which mostly explain the #
aka. four "+" symbols in a two-by-two grid) and for historical reasons, allowing empty statements was facilitating the transition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With