Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2005

Tip: Looking for answers? Try searching our database.

";" after if statement??

Thread view: 
JS - 29 May 2005 19:00 GMT
What does it mean when a ";" is inserted after an if statement?

if (x == 2){
   System.out.println("x = 2");
};
Andrew McDonagh - 29 May 2005 19:16 GMT
> What does it mean when a ";" is inserted after an if statement?
>
> if (x == 2){
>     System.out.println("x = 2");
> };

nothing...whilst it is not illegal - it is useless.
Roland - 29 May 2005 19:27 GMT
> 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.

However it can become unreachable (causing a compilation error), like in
the following example:

    public static int abs(int i) {
        if (i<0) {
            return -i;
        } else {
            return i;
        }
        ; // unreachable
    }

Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

JS - 29 May 2005 20:13 GMT
> > 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.