13

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

s4eed
  • 7,173
  • 9
  • 67
  • 104
  • 6
    Because the syntax of C or C++ is so defined. – Basile Starynkevitch Feb 20 '13 at 06:00
  • 1
    There is one case when you need to place `;` after `}` - it's class/struct/union declaration. In other hand, you **must** place `;` after each statement – borisbn Feb 20 '13 at 06:01
  • 6
    @BasileStarynkevitch: That's not an answer. Joachim Pileborg posted the answer. Yours is just noise and discourages curiosity. "Because the spec says so herp derp" answers suck. Obviously there is a *reason* for each rule. – Ed S. Feb 20 '13 at 06:07
  • 1
    @BasileStarynkevitch ~> But I think it wasn't randomly chosen :) – s4eed Feb 20 '13 at 06:19
  • @EdS. Sadly, the C and C++ standards are far for rational. There isn't necessarily a logical explanation why the standard adopted a certain syntax. Or maybe there once was one, but you would have to do programming language archaeology and find the rationale for the BCPL language or similar. – Lundin Feb 20 '13 at 07:43

4 Answers4

16

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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
12

They are two different loop constructs. You just need to remember.

Think of them as one-liners:

do one_thing while( condition );

do { stuff } while( condition );

and

while( condition ) one_thing;

while( condition ) { stuff }

These are all expressions that require a semi-colon at the end, except the last one, where the braces logically denotes the end of the expression (think of if statements).

paddy
  • 60,864
  • 6
  • 61
  • 103
0

Because It's a standard.
Refer The ISO C++ standard Page Number 128
Also
It's because while statements are valid within a do-while loop.

int x = 10;
int y = 10;

do 
 while(x > 0)
  x--;
 while(x = y--);

Because you're ending the statement.
A statement ends either with a block (delimited by curly braces),
or with a semicolon. "do this while this" is a single statement, and can't end with a block (because it ends with the "while"),
so it needs a semicolon just like any other statement.

Nagesh Salunke
  • 1,278
  • 3
  • 14
  • 37
  • 1
    The other answers explain the logic behind the standard. Perhaps you could provide your take on it too? – dcow Feb 20 '13 at 06:08
  • That is not "The ISO C++ Standard." That is the _Final Committee Draft_ of the C++11 Standard. There are many differences between that draft and the final C++11 Standard document. The closest publicly available draft to the final document is [N3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf), I believe. – James McNellis Feb 20 '13 at 08:07
  • I think its [N3485](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdf) – ForeverLearning Feb 21 '13 at 13:52
0

The reason behind this is as follows

  1. While condition:- While condition have the signature like

    while (expression) 
       statement
    

While loop executes according to the expression provided to it. If the expression is true then the statement written in while {} will execute else not. You can also mark ; after while (expression); but it will merely complete the while syntax immediately, as ; indicates end of statement. In general while loop checks expression and executes list of statements inside.

  1. Do While condition:- Do while have a signature like

    do
        statement
    while (expression);
    

do-while is an interesting loop. It have specialty that the statements following do will execute atleast ones no matter whether expression in while is true or false. The flow of do while says that the statement following do will execute once and then the while condition is checked. If while condition is true, again statements following do will be executed. The reason why there is ; at the end of do ... while (expression); is because here while is treated as a statement as because its body is above it. In c++ every statement must end with ; so do while ends with ;.

user207421
  • 305,947
  • 44
  • 307
  • 483
Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51