+ 1
What does double semicolon ";;" in C++ programming langauge?
I can not find any proper answer to my question on Internet, so I hope someone here would help me.
6 Réponses
+ 10
You know that a statement has to be ended with a semicolon, right?
A single semicolon is just an empty statement. You could string up as many as you want, and nothing would happen.
Two semicolons can also occur in for loops:
for(;;) {
...
}
In this case, all three 'slots' are empty, nothing is initialized, or incremented, and there's no condition for when the loop should run.
This is a way to write an eternal loop that can only be left from inside the block with a break statement.
+ 3
Also works similarity in C, Java and C#.
+ 2
I see, thank you! You made my day.
+ 1
I would disagree about breaking for(;;)
You can also break it with "return" or "goto" (or any terminating function like exit)
+ 1
Yeah, or by turning off your computer.
0
If you see two semicolons at the end of a statement then the second one is declring an empty statement. That's usually a mistake, but it's harmless.
Empty statements aren't always harmless though :
While (iter != svec.end()) ;
++iter;
Creates an endless loop, because the loop body is an empty statement and the ++iter is outside it.
Empty statements do have a use if the language requires a statement but your programs logic does not, like when you omit a parameter in a loop statement.