What does only ";" inside the if block means?

Why would anyone use this?

if(a==1){;} 

There is no instruction, only one semicolon, and it does not throw an error. What is the purpose of this semicolon?

+42
c
Jan 11 '17 at 2:04 on
source share
11 answers

This is an empty statement. A single semicolon alone does not perform any operations.

In this context, this means that if the if condition if true, do nothing.

Without an else section, there is not much use in this code. If so, the question is whether the condition should be inverted and should contain only the non-empty part of if .

In this case, it is a simple conditional, so in style it is probably better to invert it, however, if the condition is more complex, it may be clearer to write this path. For example, this:

 if ((a==1) && (b==2) && (c==3) && (d==4)) { ; } else { // do something useful } 

May be clearer than this:

 if (!((a==1) && (b==2) && (c==3) && (d==4))) { // do something useful } 

Or that:

 if ((a!=1) || (b!=2) || (c!=3) || (d!=4)) { // do something useful } 

Best example from comments (thanks Ben):

 if (not_found) { ; } else { // do something } 

Versus:

 if (!not_found) { // do something } 

Which method to use largely depends on what exactly is compared, how many terms are there, how the terms and even the names of variables / functions are embedded.

Another example of when you can use this is that you have a set of if..else to check a range of values ​​and you want to document in code that nothing should happen for a specific 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 an empty if was omitted, the reader might wonder if something should have happened there, and the developer forgot about it. Including an empty if , he tells the reader: "Yes, I explained it, and nothing should happen in this case."

Some coding standards require that all possible results be explicitly incorporated into the code. Thus, code adhering to such a standard might look something like this.

+54
Jan 11 '17 at 14:05
source share

He called the null operator.

From 6.8.3 Expressions and Null Statements :

The null operator (consisting of a semicolon only) does not perform any operation.

In this particular example, if(a==1){;} it does nothing useful (except, perhaps, more obvious), just like if(a==1){} or if(a==1); .

+47
Jan 11 '17 at 14:10
source share

No purpose at all. This is an empty statement. Perhaps the developer wanted to do something if a not 1 , in which case the code would be something like (even if ; you can omit it):

 if(a==1) { ; } else { //useful code... } 

But in this case it could be easily written as if(a != 1) .




Note that if it is C ++ (tags are not yet clear), then there is the possibility of operator== overloading of type a , and some side effects may be detected. That would mean the code is crazy though.

+19
Jan 11 '17 at 14:07 on
source share

This is the so-called null operator. This is a kind of expression in which the expression is missing.

Sometimes it is used in if-else statements, as you have shown, when it is easy to write an if condition than to negate it (or if condition is more clear), but the code must be executed when the if condition evaluates to false.

for example

 if ( some_condition ) { ; // do nothing } else { // here there is some code } 

Typically, the null operator is used for presentation only to show that, for example, the body of the function or the constructor of the class or the 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, a null statement is required for the while loop. You can also rewrite it as

  while ( ( *dsn++ = *src++ ) ) {} 

Sometimes a null statement is required especially in rare cases when the goto statement is used. For example, there are no statements in C declarations. If you want to transfer control of the program to the point before some declaration, you can put a shortcut before the announcement. However, in C, the label can only be placed before the statement. In this case, you can put a null statement with a label before the declaration. for example

 Repeat:; ^^^ int a[n]; //.... n++; goto Repeat; 

Pay attention to the null instruction after the label. If you delete it, the C compiler will throw an error.

This trick is also used to transfer program control to the end of a compound statement. for example

 { // some code goto Done; //... Done:; } 

Although you should not use the goto statement, you should be aware of these cases. :)

+14
Jan 11 '17 at 14:27
source share

As many have said, he does nothing.

Why is he there? Here is the opportunity ...

I am tired of the bad coders on my team without checking the return values ​​of the functions.

So, since we are developing on Linux with the GCC compiler, I am adding __attribute__((warn_unused_result)) to the declaration of all my typed functions. See also this question for MS VC equivalent.

If the user does not check the return value, the compiler generates a warning.

Cowboys, of course, played the system and the code if (foo() != SUCCESS) ; which satisfies the compiler (although it does not satisfy me :-) .

+9
Jan 11 '17 at 16:11
source share

The only one ; is a null statement that does nothing. It is often used in long if / else chains, for example:

 if (a == 1) { // Do nothing. ; } else if (...) { ... } else if (...) { ... } 

It can also be written as:

 if (a != 1) { if (...) { ... } else if (...) { ... } } 

The latter adds another level of indentation, but IMO, this is clearer. But null instructions can sometimes be useful to avoid over-indentation if there are multiple branches without code.

Note that an empty compound statement (also called a block) has the same effect:

 if (a == 1) {} 

Technically, you need a semicolon if you want to use the expression operator directly:

 if (a == 1) ; else if (...) { ... } else if (...) { ... } 

Thus, the notation {;} is redundant and serves only for a dubious illustrative purpose.

Another example is from the C standard, where the null operator is used to supply an empty loop body:

 char *s; /* ... */ while (*s++ != '\0') ; 

But here I would also prefer an empty compound statement.

+8
Jan 11 '17 at 14:32
source share

This is basically a null / empty statement that does nothing and, as expected, is a benign (error free) character.

C Draft draft N1570 explains that:

  • expressions are optional: expression [opt];
  • The null operator (consisting of a semicolon only) does not perform any operations.

On the other hand, Bjarne cites in his book in C ++ #9.2 Statement Summary :

A semicolon is an operator, an empty operator.

Note that the two if(1) {;} and if(1) {} codes have no difference in the generated gcc instructions.

+5
Jan 11 '17 at 14:07 on
source share

; in itself is an empty instruction. If a is an initialized non-pointer type, then

if(a==1){;}

always a no-op in C. Using braces adds extra weight to the statement that there is no typo. Perhaps there is an else under it, which will execute if a not 1.

+5
Jan 11 '17 at 14:10
source share

Why would you do this? I do things like this all the time when I debug to give me something that (hopefully) will not be optimized, so I can put a breakpoint on it.

Usually, although I make it obvious, the debug mark

 if( (a==1) && (b==2) && (c==3) && (d==4) ){ int debug=1; } 

Otherwise, I get weird little bits of code stuck everywhere that make absolutely no sense even to me.

But this does not guarantee what it was.

+1
Jan 12 '17 at 7:52
source share

; this is an empty noop statement

What is his purpose?

Sometimes we just want to compare things, like in this case, the equality of a and 1. With primitive types such as integer, this will set the case of conditions if you are after that if something like if (a> 1) or if (a <1) and a does not change at the same time, this may not be evaluated again depending on the decision of the compiler.

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 performed only once cmpl $ 1, 4 (% esp) , where exactly (a == 1) is written.

0
Jan 11 '17 at 20:01
source share

You probably already know that the semicolon is an empty statement. As for the actual question: β€œWhat purpose does this semicolon serve”, the answer will be (if the author of this code did not want to animate the code with some strange emoticon) that it serves absolutely the purpose in general . That is, {;} completely equivalent to {} in any context (where an assertion is expected, which, I think, is the only place where {;} can be used). The presence of a semicolon changes a compound statement with 0 compound statements to a compound statement with one compound expression, which is an empty statement, but both cases are equivalent with respect to semantics.

I will add a personal opinion that, for the convenience of reading, without being accidentally missed by the code reader, {;} not better than {} (if it was, then perhaps {;;;;;;;;} would be better), although both are significantly better than single ; (and, as a result, the language would be better without allowing it ; as an empty operator in general, since replacing it with {} always beneficial).

0
Jan 12 '17 at 8:54 on
source share



All Articles