Sign in to follow this  

What's with the double semicolon

This topic is 2131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In some old code I see this:

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


What does the ";;" after breaks do?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Tispe' timestamp='1329488801' post='4913915']
In some old code I see this:

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


What does the ";;" after breaks do?
[/quote]

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.

Share this post


Link to post
Share on other sites
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.


[code]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) [/code]

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 2131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this