In C/C++ why does the do while(expression); need a semi colon?
Because you're ending the statement. A statement ends either with a block (delimited by curly braces), or with a semicolon. "do this while this" is a single statement, and can't end with a block (because it ends with the "while"), so it needs a semicolon just like any other statement.
Why do { } while(condition); needs semicolon at the end of it but while(condition) {} doesn't?
You put semicolon after all statements, except the block statement. This is the reason that you place it after the while
in do while
, but not after the block in the while {...}
.
You also use it to terminate almost all declarations. The only exceptions I can think about at the moment is function bodies, and namespace
bodies in C++.
why there is semicolon after loop while();
The ;
is just a null statement, it is a no op but it it the body of the while loop. From the draft C99 standard section 6.8.3
Expression and null statements:
A null statement (consisting of just a semicolon) performs no operations.
and a while statement is defined as follows from section 6.8.5
Iteration statements:
while ( expression ) statement
So in this case the statement of the while loop is ;
.
The main effect of the while loop is here:
string1[i++] == string2[j++]
^^^ ^^^
So each iteration of the loop increments i
and j
until the whole condition:
string1[i++] == string2[j++] &&string1[i-1] != 0 && string2[j-1] != 0
evaluates to false
.
When is a semicolon after } mandated in c/c++?
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);
Effect of using a comma instead of a semi-colon in C and C++
It doesn't make a difference in the code you posted. In general, the comma separates expressions just like a semicolon, however, if you take the whole as an expression, then the comma operator means that the expression evaluates to the last argument.
Here's an example:
b = (3, 5);
Will evaluate 3, then 5 and assign the latter to b. So b = 5
. Note that the brackets are important here:
b = 3, 5;
Will evaluate b = 3
, then 5 and the result of the whole expression is 5, nevertheless b == 3
.
The comma operator is especially helpful in for-loops when your iterator code is not a simple i++
, but you need to do multiple commands. In that case a semicolon doesn't work well with the for-loop syntax.
Semi-Colon placed before opening bracket in a while loop
You can rewrite your code using a better formatting, which probably would make the problem clearer to you:
while(condition)
;
{
statement;
}
If condition
is true (i.e. non-0), you iterate an infinite loop.
If condition
is false (i.e. 0), execution moves forward to the following statement.
why does semicolon in c++ does not show any error when inserted at the begining of code?
You don't get an error because it isn't one.
You are terminating an empty statement. That's legal in C++ (and has been in C).
So you're example consists of 2 statements (not one):
<empty> ;
string a = "5.558" ;
This can actually be useful. For instance, the for
loop needs 3 expressions (init, condidtion and increment/step). Sometimes you don't want to pass all 3, so this is a legal code as well:
for(;;) { /* do something in this endless loop */ }
The 2 semicolons inside for are therefore separators for 3 (empty) expressions
Related Topics
How to Access a Global Variable Within a Local Scope
"Winapifamily.H: No Such File or Directory" When Compiling Sdl in Code::Blocks
How to Get Python Exception Text
How to Get a List of Files in a Folder in Which the Files Are Sorted with Modified Date Time
How to Simulate a Key Press in C++
Win32 Setforegroundwindow Unreliable
Global Variable "Count" Ambiguous
Visual Studio Project Out of Date
Passing Member Function Pointer to Member Object in C++
What Optimization Does Move Semantics Provide If We Already Have Rvo
Why Don't I Need to Specify "Typename" Before a Dependent Type in C++20
How to Determine the Correct Size of a Qtablewidget
Setting Width in C++ Output Stream
Is There a Reason Why Not to Use Link-Time Optimization (Lto)
There Are No Arguments That Depend on a Template Parameter