+ 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.

11th Dec 2019, 8:31 AM
Mikolaj Grodon
Mikolaj Grodon - avatar
6 Respostas
+ 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.
11th Dec 2019, 8:47 AM
HonFu
HonFu - avatar
+ 3
Also works similarity in C, Java and C#.
11th Dec 2019, 11:36 AM
Sonic
Sonic - avatar
+ 2
I see, thank you! You made my day.
11th Dec 2019, 8:58 AM
Mikolaj Grodon
Mikolaj Grodon - avatar
+ 1
I would disagree about breaking for(;;) You can also break it with "return" or "goto" (or any terminating function like exit)
12th Dec 2019, 9:50 PM
Sergey Knyazev
Sergey Knyazev - avatar
+ 1
Yeah, or by turning off your computer.
12th Dec 2019, 9:54 PM
HonFu
HonFu - avatar
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.
8th Jul 2024, 8:52 AM
Graham Dearsley
Graham Dearsley - avatar