Re: ";" after if statement??




"JS" wrote...
> "Roland" wrote...
>> On 29-5-2005 20:00, JS wrote:
>> > What does it mean when a ";" is inserted after an if statement?
>> >
>> > if (x == 2){
>> > System.out.println("x = 2");
>> > };
>> >
>> >
>> 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?

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





.