Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do { } while(condition); needs semicolon at the end of it but while(condition) {} doesn't? [closed]

I always have problem with placing ; at the end of while or not placing it at the end of do while loops. So what's the reason? Why

int numItemsToProcess = 3;  
while(numItemsToProcess > 0)  
{  
    // process an item  
    numItemsToProcess--;  
} 

doesn't need ; at the end but

do  
{  
    numItemsToProcess --;
} while (numItemsToProcess > 0);  

does? Maybe the reason is not too important. but when you know the reason you can remember where to put ;.

like image 707
s4eed Avatar asked Feb 20 '13 05:02

s4eed


1 Answers

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

like image 123
Some programmer dude Avatar answered Nov 02 '22 00:11

Some programmer dude