By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Empty Block require semicolon?

P: n/a
If I had some code, with several if-then-else clauses, one of them
containing an empty block, would I need a semi-colon in that block?
What effect would a semicolon have?

I ask because I have inherited some code that contains such a
construct, and I'm not sure if the blatant semicolon is required, or
was just thought to be required.
EX:

if (something1)
{
// code here;
}
else if (something2)
{
// code here;
}
else if (something3)
{
// nothing here but comments
// do I need a ";" here?
// if there is one here, what effect, if any, does it have?
// does it short-circuit anything?
}
else if (something4)
{
// code here;
}

Jun 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
wh*************@gmail.com wrote:
If I had some code, with several if-then-else clauses, one of them
containing an empty block, would I need a semi-colon in that block?
No.
What effect would a semicolon have?
Different effect in different places. In an empty block it has no
effect.
I ask because I have inherited some code that contains such a
construct, and I'm not sure if the blatant semicolon is required, or
was just thought to be required.
EX:

if (something1)
{
// code here;
}
else if (something2)
{
// code here;
}
else if (something3)
{
// nothing here but comments
// do I need a ";" here?
No.
// if there is one here, what effect, if any, does it have?
None.
// does it short-circuit anything?
Huh?
}
else if (something4)
{
// code here;
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '06 #2

P: n/a


wh*************@gmail.com wrote:
If I had some code, with several if-then-else clauses, one of them
containing an empty block, would I need a semi-colon in that block?
What effect would a semicolon have?

I ask because I have inherited some code that contains such a
construct, and I'm not sure if the blatant semicolon is required, or
was just thought to be required.

EX:

if (something1)
{
// code here;
}
else if (something2)
{
// code here;
}
else if (something3)
{
// nothing here but comments
// do I need a ";" here?
// if there is one here, what effect, if any, does it have?
// does it short-circuit anything?
}
else if (something4)
{
// code here;
}


no you don't need it. It is the end of an 'empty' statement that does
nothing. But some people put those in as
a kind of comment to clarify the block is meant to be empty , vs. that
the block accidentally had its code
deleted...

David

Jun 22 '06 #3

P: n/a
Thanks, Vic and Dave.

I was hoping that the semicolon was the source of a problem, but I
found the real culprit.
--
W

Jun 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.