We just found, a colleague and I, a strange compiling syntax with the If conditional syntax :
if (true); {
foo();
}
Is there someone here for explaining to us this strange syntax? Thx.
We just found, a colleague and I, a strange compiling syntax with the If conditional syntax :
if (true); {
foo();
}
Is there someone here for explaining to us this strange syntax? Thx.
On
to me, it looks like an if statement with an empty body, followed by a code block that always executes (and is unrelated to the if statement). For example, does foo also execute here:
if (false); {
foo();
}
On
i think this is equvalent to
if (true) {};
foo();
since the if statemnt is ended after the semicolon and the foo statement just got placed in a block ({}) ...
On
Maybe a remain from an old debugging if, or someone was planning to write a condition later but never did it...
I think you can safely remove this if statement, there's absolutely no point in doing this.
On
That's dead code. The if block is useless.
The equivalent of
if(doesn't matter whats here) {}
foo();
On
This makes no real sense but is syntactically correct. It could also be written as
if (true)
{
// do nothing
}
{
foo();
}
with the {} around foo(); meaning nothing in this case. It would limit the scope of variables defined within the {} if there were variables defined.
{
int i=0;
System.out.println(i);
}
{
String i="hello";
System.out.println(i);
}
works just fine.
On
As others have pointed out, syntactically it's a conditional with an empty execution statement (i.e. does nothing) followed by a call to foo() enclosed within scoping brackets. foo() is always executed. In practice, if you found this in a real case and not a programming test I suspect one of the following:
The first part,
if (true);is just a do-nothing conditional that ends at the;. The rest is a call tofoo()inside a new scope block. You should find thatfoo()is always called.