Empty Block require semicolon?

W

whatupbuttercup

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;
}
 
V

Victor Bazarov

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
 
D

David Lindauer

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
 
W

whatupbuttercup

Thanks, Vic and Dave.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top