Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does just ";" inside an "if" block mean? [duplicate]

Tags:

c

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?

like image 246
Nadeem Khan Avatar asked Jan 11 '17 14:01

Nadeem Khan


People also ask

What is a nested IF statement?

Nested IF functions, meaning one IF function inside of another, allow you to test multiple criteria and increases the number of possible outcomes.

What does the comma mean in an if statement?

The programmer has used the comma operator to provide two unrelated expressions in a single statement. Because it's a single statement, both expressions are "inside" the if condition.

Can we keep else block empty?

There is no functional reason to have an empty else block, and the compiler will likely just optimize it away.

What is nested IF with example?

For example, every person is eligible to work if he is 18 years old or above else he is not eligible. However, companies will not give a job to every person. So, we use another IF Statement, also called as Nested If Statement in C, to check his education qualifications or any specific company requirements.


2 Answers

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.

like image 103
dbush Avatar answered Oct 04 '22 16:10

dbush


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);.

like image 32
P.P Avatar answered Oct 04 '22 16:10

P.P