
Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
> > What does it mean when a ";" is inserted after an if statement?
> >
[quoted text clipped - 3 lines]
> >
> It's a so-called empty statement. It does nothing.
Ok if I put a ";" after an if-statement that if statment will not be
evaluated?
Roland - 29 May 2005 20:45 GMT
>>>What does it mean when a ";" is inserted after an if statement?
>>>
[quoted text clipped - 6 lines]
> Ok if I put a ";" after an if-statement that if statment will not be
> evaluated?
No, the if statement of your example *will* be executed (that is: the
test x==2, and depending on its outcome also the println).
It's like
if (x == 2){
System.out.println("x = 2");
}System.out.print("foo");
but without System.out.print("foo")
And this is similar to
if (x == 2){
System.out.println("x = 2");
}
System.out.print("foo");
The <if> statement will be executed (the test and optionally the
println), and also the statement <System.out.print("foo");> will executed.
Likewise your example is similar to
if (x == 2){
System.out.println("x = 2");
}
;
The <if> statement will be executed (the test etc), and also the empty
statement <;> will executed. But the empty statement does nothing, so
nothing will change.
So far the analogy.
Because an empty statement doesn't do a thing, the Java compiler will
not generate code for it (any smart Java compiler, that is; a "dumb"
compiler might generate some no-op code for it).

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
Bjorn Abelli - 30 May 2005 10:44 GMT
"JS" wrote...
> "Roland" wrote...
>> > What does it mean when a ";" is inserted after an if statement?
[quoted text clipped - 7 lines]
> Ok if I put a ";" after an if-statement that if statment
> will not be evaluated?
It will be evaluated, but notice the difference between the following
constructions:
-----------------------------------
if (x == 2)
System.out.println("x = 2");
-----------------------------------
if (x == 2);
System.out.println("x = 2");
-----------------------------------
They look very similar, don't they... ;-)
A misplaced empty statement can make a logic error in the code which can be
difficult to discover.
In both cases the condition is evaluated, and hence the following statement
is executed. But in the second example the following statement is the
*empty* statement. The printout in the latter example will always execute,
as it's not a part of the if-block.
// Bjorn A
Tor Iver Wilhelmsen - 30 May 2005 15:39 GMT
> A misplaced empty statement can make a logic error in the code which can be
> difficult to discover.
Which IMHO is why Java should have required braces for if, while, do
etc. like it does for try, catch, finally, synchronized.
Fred L. Kleinschmidt - 31 May 2005 16:39 GMT
> > > What does it mean when a ";" is inserted after an if statement?
> > >
[quoted text clipped - 6 lines]
> Ok if I put a ";" after an if-statement that if statment will not be
> evaluated?
As others have said, that semicolon does nothing. The code fragment is
the same as:
if (x == 2){
System.out.println("x = 2");
}
;
Note that the following is also legal:
if (x == 2){
System.out.println("x = 2");
};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
y = 5;;;;;;;;
It just contains a whole bunch of useless empty statements.

Signature
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225