if(...) {
...
}
It seems in the above case a ;
is optional,when is a semicolon after }
necessary in c/c++?
Semicolon there is NOT optional.
while' loop syntax needs a semicolon at the end. Whereas for and while loop do not need a semi-colon terminator at end.
You have to put the semicolon there so the compiler will know whether you declared any instances or not. This is a C compatibility thing. Doesn't even need the name or constituents: class { } waldo; is legal.
Role of Semicolon in C: Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements.
int a[2] = {1,2}, j = 5;
When initialization of array or structure are done with {}
all the subsequent variables are declared after ,
.
Edit: As you changed your question; ;
is mandatory after the class
, enum
, initialization syntax declarations.
class A {}; // same for `struct
enum E {}; // enum class (c++0x)
int a[] = {1,2}; // array or object initialization
And as per comment by @rubenvb:
do {} while(condition);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With