Why would someone use this?
if(a==1){;}
There no statement, only one semicolon, and it doesn't throw an error. What purpose does this semicolon serve?
Why would someone use this?
if(a==1){;}
There no statement, only one semicolon, and it doesn't throw an error. What purpose does this semicolon serve?
It's an empty statement. A single semicolon by itself performs no operation.
In this context, it means that if the if
condition is true, do nothing.
Without an else
section, there is not much use to this code. If there is, then it's a matter of style whether the condition should be inverted and should contain just a non-empty if
portion.
In this case it's a simple conditional, so style-wise it's probably better to invert it, however if the condition is more complicated it may be clearer to write this way. For example, this:
if ((a==1) && (b==2) && (c==3) && (d==4)) {
;
} else {
// do something useful
}
Might be clearer than this:
if (!((a==1) && (b==2) && (c==3) && (d==4))) {
// do something useful
}
Or this:
if ((a!=1) || (b!=2) || (c!=3) || (d!=4)) {
// do something useful
}
A better example from the comments (thanks Ben):
if (not_found) {
;
} else {
// do something
}
Versus:
if (!not_found) {
// do something
}
Which method to use depends largely on exactly what is being compared, how many terms there are, how nested the terms are, and even the names of the variables / functions involved.
Another example of when you might use this is when you have a set of if..else
statements to check a range of values and you want to document in the code that nothing should happen for a particular range:
if (a < 0) {
process_negative(a);
} else if (a >=0 && a < 10) {
process_under_10(a);
} else if (a >=10 && a < 20) {
; // do nothing
} else if (a >=20 && a < 30) {
process_20s(a);
} else if (a >= 30) {
process_30s_and_up(a);
}
If the empty if
was left out, a reader might wonder if something should have happened there and the developer forgot about it. By including the empty if
, it says to the reader "yes I accounted for this and nothing should happen in this case".
Certain coding standards require that all possible outcomes be explicitly accounted for in code. So code adhering to such a standard might look something like this.
It's called null statement.
From 6.8.3 Expression and null statements:
A null statement (consisting of just a semicolon) performs no operations.
In this particular example if(a==1){;}
, it doesn't do anything useful (except perhaps a bit more obvious) as it's same as if(a==1){}
or if(a==1);
.
No purpose at all. It's an empty statement. It's possible that the developer wanted to do something if a
was not 1
in which case the code would be something like (even though the ;
can still be omitted) :
if(a==1)
{
;
}
else
{
//useful code...
}
But in that case, it could've easily been written as if(a != 1)
.
Note however, if this is C++(tags aren't clear yet) then there would be a possibility that the operator==
of type a
was overloaded and some side effects could've been observed. That would mean that the code is insane though.
It is so called null statement. It is a kind of the expression statement where the expression is missed.
Sometimes it is used in if-else statements as you showed when it is easy to write the if condition than its negation (or the if condition is more clear) but the code should be executed when the if condition is evaluated to false.
For example
if ( some_condition )
{
; // do nothing
}
else
{
// here there is some code
}
Usually the null statement is used for exposition only to show that for example body of a function, or a class constructor or a for loop is empty. For example
struct A
{
A( int x ) : x( x )
{
;
}
//...
};
Or
char * copy_string( char *dsn, const char *src )
{
char *p = dsn;
while ( ( *dsn++ = *src++ ) ) ;
^^^
return p;
}
Here the null statement is required for the while loop. You could also rewrite it like
while ( ( *dsn++ = *src++ ) ) {}
Sometimes the null statement is required especially in rare cases when goto statement is used. For example in C declarations are not statements. If you want to pass the program control to a point before some declaration you could place a label before the declaration. However in C a label may be placed only before a statement. In this case you could place a null statement with a label before the declaration. For example
Repeat:;
^^^
int a[n];
//....
n++;
goto Repeat;
Pay attention to the null statement after the label. If to remove it then the C compiler will issue an error.
This trick also is used to pass the program control to the end of a compound statement. For example
{
// some code
goto Done;
//...
Done:;
}
Though you should not use the goto statement but you should know about these cases.:)
As so many have said - it does nothing.
Why is it there? Here is a possibility …
I am fed up with bad coders on my team not checking return values of functions.
So, since we develop in Linux with the GCC compiler, I add __attribute__((warn_unused_result))
to the declaration of all of my typed functions. See also this question for a MS VC equivalent.
If the user does not check the return value, then the compiler generates a warning.
The cowboys, of course, have gamed the system and code if (foo() != SUCCESS) ;
which satisfies the compiler (although it does not satisfy me :-).
A single ;
is the null statement which does nothing. It's often used in long if/else chains like:
if (a == 1) {
// Do nothing.
;
}
else if (...) {
...
}
else if (...) {
...
}
This could also be written as:
if (a != 1) {
if (...) {
...
}
else if (...) {
...
}
}
The latter adds another level of indentation but IMO, it's clearer. But null statements can sometimes be useful to avoid excessive indentation if there are multiple branches without code.
Note that an empty compound statement (also called block) has the same effect:
if (a == 1) {}
Technically, you only need the semicolon if you want to use an expression statement directly:
if (a == 1) ;
else if (...) { ... }
else if (...) { ... }
So writing {;}
is redundant and only serves a dubious illustrative purpose.
Another example from the C standard where the null statement is used to supply an empty loop body:
char *s;
/* ... */
while (*s++ != '\0')
;
But here I'd also prefer an empty compound statement.
It is basically an null/empty statement, which does nothing, and is as expected benign (no error) in nature.
C Spec draft N1570 clarifies that:
- the expressions are optional: expression[opt] ;
- A null statement (consisting of just a semicolon) performs no operations.
On the other hand, Bjarne quotes in his C++ book #9.2 Statement Summary
:
A semicolon is by itself a statement, the empty statement.
Note that these two codes, if(1) {;}
and if(1) {}
, have no difference in the generated gcc instructions.
;
on its own is an empty statement. If a
is an initialised non-pointer type, then
if(a==1){;}
is always a no-op in C. Using the braces adds extra weight to the assertion that there is no typo. Perhaps there is an else
directly beneath it, which would execute if a
was not 1.
Why would you do it? I do similar things all the time when debugging to give me something that (I hope) won't be optimized away so I can put a break point on it.
Normally though I make it obvious its a debug mark
if( (a==1) && (b==2) && (c==3) && (d==4) ){
int debug=1;
}
Otherwise I end up with weird little bits of code stuck everywhere that make absolutely no sense even to me.
But that is not guarantee that's what this was.
; is an empty statement noop
What is its purpose?
Sometimes we just want to compare things like in this case the equality of a and 1. With primitive types like integer, this will set condition register so if you have after this if something like if(a > 1) or if(a < 1), and a is not changed in the meantime, this might not be evaluated again depending on the compiler decision.
This code
int f(int a){
if(a == 1){;}
if(a > 1){return 3;}
if(a < 1){return 5;}
return 0;
}
will give
.file "c_rcrYxj"
.text
.p2align 2,,3
.globl f
.type f, @function
f:
.LFB0:
.cfi_startproc
cmpl $1, 4(%esp)
jle .L6
movl $3, %eax
ret
.p2align 2,,3
.L6:
setne %al
movzbl %al, %eax
leal (%eax,%eax,4), %eax
ret
.cfi_endproc
.LFE0:
.size f, .-f
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
.section .note.GNU-stack,"",@progbits
The comparison is done only once cmpl $1, 4(%esp), exactly where if(a==1) is written.
You probably already know that the semicolon is an empty statement. Concerning the actual question "what purpose does this semicolon serve", the answer is (unless the author of this code wanted to enliven the code with some weird emoticon), that it serves absolutely no purpose at all. That is, {;}
is completely equivalent to {}
in any context (where a statement is expected, which I think is the only place where {;}
can be used). The presence of the semicolon changes a compound statement with 0 constituent statements into a compound statement with 1 constituent statement which is an empty statement, but the two cases are equivalent as far as semantics is concerned.
I'll add a personal opinion that for readability concern of not being accidentally overlooked by a reader of the code, {;}
is no better than {}
(if it were, then maybe {;;;;;;;;}
would be better still), though both are considerably better than a lone ;
(and as a consequence the language would have been better off without allowing ;
as empty statement at all, since replacing it with {}
is always advantageous).