I tried to search for good resources on empty statement, but it seem like nothing show up. Even on MDN, they don't have much to say about it.
i.e:
for(var i = 0; i < a.length; a[i++] = 0);
if((a==0) || (b == 0));
I would like to know what are some real examples that one should use empty statements on their project. What are the reason behind it?
A statement is an instruction given to the computer to perform any kind of action. An empty statement is useful in situations where the code requires a statement but does not require logic. To fill these two requirements simultaneously an empty statement is used.
Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.
PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
An empty statement in Java is written as a single semicolon. The empty statement doesn't do anything, but the syntax is occasionally useful. For example, you can use it to indicate an empty loop body of a for loop: for(int i = 0; i < 10; a[i++]++) // Increment array elements /* empty */; // Loop body is empty statement.
The examples you've given don't make much sense. They should better be written
for (var i = 0; i < a.length;) a[i++] = 0;
for (var i = 0; i < a.length; i++) a[i] = 0;
; // the comparisons really don't do anything (assuming a and b are no objects)
(a==0) || (b = 0); // Oh wait, that's the one given by @Shomz
if (a != 0) b = 0;
However, there are real-world applications for the empty statement. I'll just list 3 that come to my mind:
function x() {
…
};
A semicolon where it doesn't belong (e.g. after the function declaration above) makes an empty statement.
;
…
A leading semicolon on your script files helps to guard against buggy inclusions or file concatenations.
while (!check_for_finish()); // do nothing
An empty loop body can be used for busy-waiting loops (not recommended) and similar.
none/lazyness. there is absolutely no difference to
for(var i = 0; i < a.length;) a[i++] = 0;
and just a minimal difference to
for(var i = 0; i < a.length; i++) a[i] = 0;
the first one is a few ms faster after a few billion iteration steps; aka. premature optimization
EDIT:
if((a==0) || (b == 0));
this makes no sense at all, since it does nothing.
but expresions like
a==0 || (b=0);
//or maybe sth like this:
//var noop = ()=>void 0; //FYI
typeof a === "function" || (a = noop);
are pretty useful to me, since they are short, and readable and an additional if-statement doesn't add any value to readability or understanding (at least once you know this pattern).
The first one obviously loops through the array and assigns all values to zero, without having the code specified in the statement.
The other one seems like a typo, because it is useless.
However, something like
if((a==0) || (b = 0));
would make sense, as it would assign b
to zero in case a
is not zero.
var a = 1, b = 1;
if((a == 0) || (b = 0));
alert("a: " + a + ", b: " + b);
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