Skip to main content
GameDev.net gamedev.net
🔒 Locked

What's with the double semicolon

Started by Tispe Feb 17, 2012 at 2:26 PM 6 replies 17.8k views
Original Post
Tispe
Tispe
In some old code I see this:


for(;;){
.
.
.
.
if( /*someting*/ ) break;;
test1 = 1; test2 = 2;
if( /*someting*/ ) break;;
return;
.
.
.
.



What does the ";;" after breaks do?
DarrenHorton
DarrenHorton
IN C# it is for an infinite loop

the break and return statements are how the infinite loop is broken out of.
frob
frob
As written, nothing. The extra semicolon after the break statement is an empty statement.
BeerNutts
BeerNutts
Your topic sounds like a Jerry Seinfeld skit. "What's up with all these semi-colons? Is one not enough? I mean, come on!"

As to your question, it's an empty statement; it does nothing. It's probably a typo, and a copy-paste issue.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
Tispe
Tispe
Thanks, I was starting to think that it would run the next statement before breaking or something....
slicer4ever
slicer4ever

In some old code I see this:


for(;;){
.
.
.
.
if( /*someting*/ ) break;;
test1 = 1; test2 = 2;
if( /*someting*/ ) break;;
return;
.
.
.
.



What does the ";;" after breaks do?


the only situation here is the for(;;) essentially, it's an infinite loop using the for loop, since it lacks any statements, the compiler assumes the loop is true, and continues executing the loop.

essentially, this is a theoretical faster implementation of while(1), since a while loop is always tested conditionally, a for(;;) is assumed true, unless an condition exists, the performance gain is minimalistic at best, but it does exist, and is why you see it.
epreisz
epreisz
Compilers are really smart these days. I ran a quick test on both while(1) and for(;;) and they generate the exact same assembly. So to the processor...they are the same.


while(1)
001B1000 push esi
001B1001 push edi
001B1002 mov edi,dword ptr [__imp__rand (1B20A0h)]
001B1008 xor esi,esi
001B100A lea ebx,[ebx]
001B1010 call edi
001B1012 add esi,eax
001B1014 cmp esi,3E8h
001B101A jle wmain+10h (1B1010h)

for(;;)
00FB1000 push esi
00FB1001 push edi
00FB1002 mov edi,dword ptr [__imp__rand (0FB20A0h)]
00FB1008 xor esi,esi
00FB100A lea ebx,[ebx]
00FB1010 call edi
00FB1012 add esi,eax
00FB1014 cmp esi,3E8h
00FB101A jle wmain+10h (0FB1010h)


Remember, while C++ is considered a lower level language, it's still a high level language compared to assembly that the C++ generates. If you want to look at the assembly, simply run the program in release mode, set a breakpoint and choose "dissasembly" from Debug->Windows->Dissasembly.
zacaj
zacaj
If I had to take a guess, Id say that they were used in debugging. If you have an if statement without any {} and you remove the statement after it (the break;) then it would only do the second check if the first was true. Putting the second semicolon in means that it would execute the empty statement instead of the next if, and Im guessing that then the programmer was developing it at somepoint he was removing the breaks and didnt want to remove the if statement every time too

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.