Re: ";" after if statement??
- From: "Bjorn Abelli" <bjorn_abelli@xxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 May 2005 11:44:13 +0200
"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
.
- Follow-Ups:
- Re: ";" after if statement??
- From: Tor Iver Wilhelmsen
- Re: ";" after if statement??
- References:
- ";" after if statement??
- From: JS
- Re: ";" after if statement??
- From: Roland
- ";" after if statement??
- Prev by Date: Re: report generation in java
- Next by Date: Vector#toArray()
- Previous by thread: Re: ";" after if statement??
- Next by thread: Re: ";" after if statement??
- Index(es):