3

I had an if statement checking some value, And encountered a weird bug(Not sure!). My code was incorrect syntactically and in result it produced a wrong result, however eclipse didn't raised any error while compiling. Why My below code worked?

if((this.trackPointList.get(point).getTurnOutId().equals(seg.getSegRef().getTurnOut())) && seg.getSegRef().getKind().equals("arc")); // <---- See here I have semicolon
    {
       ... code to run ...
    }

Above code check only first condition and ignores seg.getSegRef().getKind().equals("arc") but I guess this should raised an issue at compile time, Am I right? My logic worked once I debugged it by skimming line by line and found this semicolon. I will appreciate if someone could explain, if it is a valid syntax.

Enlighten Me, Please!

TeaCupApp
  • 11,055
  • 16
  • 65
  • 143

5 Answers5

11

The ; makes Java think that the body of the if conditional is complete, even if there is no other code preceding it. In effect, the code in the if statement is executed, but no body exists because the ; is there.

The { ...code to run...} is just a code block that executes, and anything declared inside that block is not visible outside the block. It will always run here because it's not part of the if block.

edit: here's another stack overflow question about the { } blocks: What do curly braces in Java mean by themselves?

Community
  • 1
  • 1
wkl
  • 68,357
  • 14
  • 154
  • 169
  • Now this makes sense :), I thought it is syntactically incorrect Thank you stacks(I like stack instead of heaps ;) ) – TeaCupApp Sep 23 '11 at 02:25
2

The code is syntactically correct. You can write ifs without braces, like:

if(condition) statement;

Having empty statements is also valid. For instance this code is valid:

int a = 0;;
;;;

So an empty if is valid as well, although it doesn't make much sense :)

if(condition);
MaxM
  • 138
  • 1
  • 8
2

An if statement followed by a semicolon is called an "empty if statement". It rarely is of any use, but it's syntactically legal.

You can write something like this

if ( doSomethingThatReturnsABoolean() )
   ;  // Empty statement
else
   doSomeOtherThing()

but it would be better to write

if ( !doSomethingThatReturnsABoolean() )
   doSomeOtherThing()

Regarding your observation that only the first condition gets checked: If the first condition returns false the second condition will not get checked, because

(false && secondCondition)

always equals to false, so the value of secondCondition is irrelevant.

Dario Seidl
  • 2,796
  • 1
  • 27
  • 43
1

The ; after the if(..) statement represents an empty statement that is executed conditionally when the if(..) evaluates to true. The { .. } represents a code block that always executes with it's own scope.

The second condition may be ignored if the first condition is false due to short-circuit evaluation.

Rusty Fausak
  • 6,755
  • 1
  • 24
  • 37
1

if(condition); is equivalent to if(condition){}

same thing with for loop & while loop:

for(;condition;); is equivalent to for(;condition;){}

while(condition); is equivalent to while(condition){}

Eng.Fouad
  • 107,075
  • 62
  • 298
  • 390