 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JS Guest
|
Posted: Sun May 29, 2005 6:00 pm Post subject: ";" after if statement?? |
|
|
What does it mean when a ";" is inserted after an if statement?
if (x == 2){
System.out.println("x = 2");
};
|
|
| Back to top |
|
 |
Andrew McDonagh Guest
|
Posted: Sun May 29, 2005 6:16 pm Post subject: Re: ";" after if statement?? |
|
|
JS wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Roland Guest
|
Posted: Sun May 29, 2005 6:27 pm Post subject: Re: ";" after if statement?? |
|
|
On 29-5-2005 20:00, JS wrote:
| Quote: | 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
}
--
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ /_/ /
|
|
| Back to top |
|
 |
JS Guest
|
Posted: Sun May 29, 2005 7:13 pm Post subject: Re: ";" after if statement?? |
|
|
"Roland" <roland (AT) phony (DOT) biz> skrev i en meddelelse
news:429a097f$0$17211$e4fe514c (AT) news (DOT) xs4all.nl...
| Quote: | 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?
|
|
| Back to top |
|
 |
Roland Guest
|
Posted: Sun May 29, 2005 7:45 pm Post subject: Re: ";" after if statement?? |
|
|
On 29-5-2005 21:13, JS wrote:
| Quote: | "Roland" <roland (AT) phony (DOT) biz> skrev i en meddelelse
news:429a097f$0$17211$e4fe514c (AT) news (DOT) xs4all.nl...
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?
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).
--
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ /_/ /
|
|
| Back to top |
|
 |
Bjorn Abelli Guest
|
Posted: Mon May 30, 2005 9:44 am Post subject: Re: ";" after if statement?? |
|
|
"JS" wrote...
| Quote: | "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
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Mon May 30, 2005 2:49 pm Post subject: Re: ";" after if statement?? |
|
|
"Bjorn Abelli" <bjorn_abelli (AT) DoNotSpam (DOT) hotmail.com> writes:
| Quote: | 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.
|
|
| Back to top |
|
 |
Fred L. Kleinschmidt Guest
|
Posted: Tue May 31, 2005 3:39 pm Post subject: Re: ";" after if statement?? |
|
|
JS wrote:
| Quote: |
"Roland" <roland (AT) phony (DOT) biz> skrev i en meddelelse
news:429a097f$0$17211$e4fe514c (AT) news (DOT) xs4all.nl...
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?
|
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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|